Question

22.2 Solve the following problem over the interval from x = 0 to 1 using a step size of 0.25 where y(0)-1. Display all your r

I want Matlab code.

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

%%%Matlab code

clc;
close all;
clear all;
f=@(x , y) (1+4*x)*sqrt(y);
y1(1)=1;
x(1)=0;
h=0.25;
t=0:h:1;
%%% Analytical method
[t1 y1]=ode45(f,[0 1],y1(1));

%%% Eular method
y2(1)=1;
for n=1:length(t)-1;
y2(n+1)=y2(n)+h*f(t(n),y2(n));
end

%%%% Hen's Method
y3(1)=1;
for n=1:length(t)-1
yp=h*f(t(n),y3(n));
y3(n+1)=y3(n)+h/2*(f(t(n),y3(n))+f(t(n),yp));
end


%%%% RK-Method
yr(1)=1;
for k=1:length(t)-1
  
k1=h*feval(f,t(k),yr(k));
k2=h*feval(f,t(k)+h/2,yr(k)+k1/2);
k3=h*feval(f,t(k)+h/2,yr(k)+k2/2);
k4=h*feval(f,t(k)+h,yr(k)+k3);
yr(k+1)=yr(k)+(k1+2*k2+2*k3+k4)/6;
end


%%% Ralstan method
yrl(1)=1;
for k=1:length(t)-1
k1=h*f(t(k),yrl(k));
k2= h*f(t(k) + 2 *h / 3, yrl(k) + 2* k1 / 3 );
yrl(k+1)=yrl(k)+1/4*(k1+3*k2);
end

figure;
plot(t1,y1);
hold on
plot(t,y2,'-*');
hold on
plot(t,y3,'-o');
hold on
plot(t,yr','-^')
hold on
plot(t,yrl);
grid on
legend('Exact solution','Eular method','Hen''s Method','RK-4 method','Ralston method');
xlabel('x');
ylabel('y');

OUTPUT:

Exact solution Eular method -Hens Methood 6 RK-4 method Ralston method 5 2 0.3 0.6 0.8 0.1 0.2 0.4 0.5 0.7 0.9

Add a comment
Know the answer?
Add Answer to:
I want Matlab code. 22.2 Solve the following problem over the interval from x = 0 to 1 using a step size of 0.25 whe...
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