Question

Numerical Methods for Differential Equations - Please post full correct solution!!! - need to use MATLAB

3. (a) Write Matlab functions to integrate the initial value problem y = f(x,y), y(a) = yo, on an interval [a, b] using: • Eu

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We have developed a MATLAB code for the given problem.

-------------------------MATLAB Code-------------------------

f=@(x,y) -0.5*y.^3; % f(x,y)
fe=@(x) 1./(sqrt(1+x)); % exact function
a=0;
y0=1;
b=1;
stepsize=[1/40 1/80];
yexact=fe(b);

for i=1:2
h=stepsize(i);
tic;[x1,y_eul]=euler(f,a,y0,b,h);T1=toc;
fprintf('\n\nEuler method: stepsize=%g\n',h);
fprintf('---------------------------------------------\n');
fprintf('y(%g)=%.8f\t error=%.10f\t Time=%g sec\n',b,y_eul,abs(y_eul-yexact),T1);
tic;[x2,y_mod]=eulermodified(f,a,y0,b,h);T2=toc;
fprintf('\n\nModified Euler method: stepsize=%g\n',h);
fprintf('---------------------------------------------\n');
fprintf('y(%g)=%.8f\t error=%.10f\t Time=%g sec\n',b,y_mod,abs(y_mod-yexact),T2);
tic;[x3,y_imp]=eulerimproved(f,a,y0,b,h);T3=toc;
fprintf('\n\nImproved Euler method: stepsize=%g\n',h);
fprintf('---------------------------------------------\n');
fprintf('y(%g)=%.8f\t error=%.10f\t Time=%g sec\n',b,y_imp,abs(y_imp-yexact),T3);
tic;[x4,y_rk4]=RK4(f,a,y0,b,h);T4=toc;
fprintf('\n\nRK4 method: stepsize=%g\n',h);
fprintf('---------------------------------------------\n');
fprintf('y(%g)=%.8f\t error=%.10f\t Time=%g sec\n',b,y_rk4,abs(y_rk4-yexact),T4);
end

function [x,y]=euler(f,a,y0,b,stepsize)
  
h=stepsize;
xn=a:h:b;
yn=0*xn; % initializing the array;
yn(1)=y0;
  
for i=1:numel(xn)-1
yn(i+1)=yn(i)+h*f(xn(i),yn(i));
end
x=xn(end);
y=yn(end);
end

function [x,y]=eulermodified(f,a,y0,b,stepsize)
  
h=stepsize;
xn=a:h:b;
yn=0*xn; % initializing the array;
yn(1)=y0;
  
for i=1:numel(xn)-1
yp=yn(i)+h*f(xn(i),yn(i));
yn(i+1)=yn(i)+h*f(xn(i)+0.5*h,yp);
end
x=xn(end);
y=yn(end);
end

function [x,y]=eulerimproved(f,a,y0,b,stepsize)
  
h=stepsize;
xn=a:h:b;
yn=0*xn; % initializing the array;
yn(1)=y0;
  
for i=1:numel(xn)-1
yp1=f( xn(i),yn(i) );
yp2=f( xn(i+1),yn(i)+h*yp1 );
yn(i+1)=yn(i)+0.5*h*( yp1+yp2 );
end
x=xn(end);
y=yn(end);
end

function [x,y]=RK4(f,a,y0,b,stepsize)
  
h=stepsize;
xn=a:h:b;
yn=0*xn; % initializing the array;
yn(1)=y0;
  
for i=1:numel(xn)-1
k1 = f(xn(i),yn(i));
k2 = f(xn(i)+0.5*h,yn(i)+0.5*h*k1);
k3 = f((xn(i)+0.5*h),(yn(i)+0.5*h*k2));
k4 = f((xn(i)+h),(yn(i)+k3*h));
  
yn(i+1) = yn(i) + (1/6)*(k1+2*k2+2*k3+k4)*h;
end
x=xn(end);
y=yn(end);
end

-------------------------MATLAB Output-------------------------

Euler method: stepsize=0.025 y (1)=0.70478126 error=0.0023255216 Time=0.0021672 sec Modified Euler method: stepsize=0.025 - -

Euler method: stepsize=0.0125 -- - - y (1)=0.70595109 error=0.0011556953 Time=0.0006278 sec Modified Euler method: stepsize=0

Add a comment
Know the answer?
Add Answer to:
Numerical Methods for Differential Equations - Please post full correct solution!!! - need to use MATLAB...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT