In: Advanced Math
Use eulers Method with step size h=.01 to approximate the solution to the initial value problem y'=2x-y^2, y(6)=0 at the points x=6.1, 6.2, 6.3, 6.4, 6.5
%%Matlab code for Euler method
clear all
close all
%Function for ode
f=@(x,y) 2.*x-y.^2;
%step size
h=0.01;
%x intervals
x_in=6; x_end=7;
%initial y
y_in=0;
%equally spaced interval
x_int=6.1:0.1:6.5;
%creating the table for Euler solution
[x,y]=euler(f,x_in,x_end,y_in,h);
for j=1:length(x_int)
p=find(abs(x-x_int(j))<10^-5);
y_data=y(p);
fprintf('\tAt x=%f value
of y(%f) is %f\n',x(p),x(p),y(p))
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function for Euler ode solution
%Euler method
function [x,y]=euler(f,x_in,x_end,y_in,h)
%function (i)
%f=@(x,y) 50*exp(-10*x)-2*y;
%initial and final x
%x_in=0; x_end=2;
%y_in=40;
%initial conditions
x(1)=x_in;
y(1)=y_in;
x_max=x_end;
%Final x
N=(x_max-x_in)/h; %number of steps
%Euler iterations
for i=1:N
x(i+1)=x_in+i*h;
y(i+1)=double(y(i)+h*(f(x(i),y(i))));
end
end
%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%