Using Matlab, solve the following ODE using Euler's method...
I have to perform solve the ODE with both step sizes and plot both on the same graph.
y'=1/y, Initial Condition y(0)=1. step size = 0.1 and 0.01
The interval is from 0 to 1.
UPDATE: I actually figured it out myself! THANKS
% We will solve this problem by Euler's Method
clc;
clear all;
h = 0.1; % initial step size
for j=1:2
x = 0:h:1; % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = 1; % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=1:n-1
f = 1/(y(i)); % The given function
y(i+1) = y(i) + h * f; % formula for Euler's method.
end
plot(x,y)
hold on
h=h*0.1;
end
Get Answers For Free
Most questions answered within 1 hours.