matlab - Implementing Euler's method for solving differential equations -


this code find euler's method on matlab

function [x,y]=euler_forward(f,xinit,yinit,xfinal,n)  h=(xfinal-xinit)/n; % initialization of x , y column vectors x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)]; % calculation of x , y i=1:n x(i+1)=x(i)+h; y(i+1)=y(i)+h*f(x(i),y(i)); end end` 'f=@(x,y) (1+2*x)*sqrt(y); % calculate exact solution g=@(x,y) (1+2*x)*sqrt(y); xe=[0:0.01:1]; ye=g(xe); [x1,y1]=euler_forward(f,0,1,1,4);  % plot plot(xe,ye,'k-',x1,y1,'k-.') xlabel('x') ylabel('y') legend('analytical','forward') % estimate errors error1=['forward error: ' num2str(-100*(ye(end)-y1(end))/ye(end)) '%'];  error={error1} 

so have far problem , gives error saying y not defined. do?

the problem here:

% calculate exact solution g=@(x,y) (1+2*x)*sqrt(y); 

exact solution function of 1 variable, x. presumably, supposed find paper , pencil (or wolfram alpha, etc):

 g=@(x) .25*(x.^2+x+2).^2  

then code works expected, producing neat chart: typical euler's method, numerical solution lags behind exact one.

output


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -