Question

1. Consider the following initial value problem 3t2-y, y(0) = 1 Using Eulers Method and the second order Runge-Kutta method,

Complete using MatLab

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

%%%%%%%%%%%% Solution of (a) and (b) by Euler

clc;
clear all;

f=@(t,y) 3*t^2-y;
a=0;% starting point
b=1; %ending point
h=0.05; % step length


n=(b-a)/h;
y(1)=1; % Initial condition
t=a:h:b;

%Euler formula
for i=1:n
y(i+1)=y(i)+h*f(t(i),y(i));
end
disp('Approximation solution')
for i=1:n+1
fprintf('y(%f)= \t %10f \n',t(i),y(i))
end
%True solution
for i=1:n+1
y_ex(i)=3*t(i)^2-6*t(i)-5*exp(-t(i))+6;
end
  
plot(t,y,'-r*')
hold on
plot(t,y_ex,'-m')

xlabel('t','fontsize',15)

ylabel('y','fontsize',15)
legend('Euler method','True solution')

disp('________________________________________________________________________________')
disp('t Euler solution True solution ')
disp('________________________________________________________________________________')

for i=1:n+1
fprintf('%f \t %10f \t %15f \n',t(i),y(i), y_ex(i))
end

Approximation solution
y(0.000000)=    1.000000
y(0.050000)=    0.950000
y(0.100000)=    0.902875
y(0.150000)=    0.859231
y(0.200000)=    0.819645
y(0.250000)=    0.784662
y(0.300000)=    0.754804
y(0.350000)=    0.730564
y(0.400000)=    0.712411
y(0.450000)=    0.700790
y(0.500000)=    0.696126
y(0.550000)=    0.698820
y(0.600000)=    0.709254
y(0.650000)=    0.727791
y(0.700000)=    0.754776
y(0.750000)=    0.790538
y(0.800000)=    0.835386
y(0.850000)=    0.889616
y(0.900000)=    0.953511
y(0.950000)=    1.027335
y(1.000000)=    1.111343
________________________________________________________________________________
t Euler solution True solution
________________________________________________________________________________
0.000000    1.000000    1.000000
0.050000    0.950000    0.951353
0.100000    0.902875    0.905813
0.150000    0.859231    0.863960
0.200000    0.819645    0.826346
0.250000    0.784662    0.793496
0.300000    0.754804    0.765909
0.350000    0.730564    0.744060
0.400000    0.712411    0.728400
0.450000    0.700790    0.719359
0.500000    0.696126    0.717347
0.550000    0.698820    0.722751
0.600000    0.709254    0.735942
0.650000    0.727791    0.757271
0.700000    0.754776    0.787073
0.750000    0.790538    0.825667
0.800000    0.835386    0.873355
0.850000    0.889616    0.930425
0.900000    0.953511    0.997152
0.950000    1.027335    1.073795
1.000000    1.111343    1.160603
>> * - Euler method True solution 0.6 L 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0. 9 1

%%%%%%%%%%%% Solution of (a) and (b) by Rk2

clc;
clear all;

f=@(t,y) 3*t^2-y;
a=0;% starting point
b=1; %ending point
h=0.05; % step length
n=(b-a)/h;
y(1)=1; % Initial condition
t=a:h:b;


for i=1:n
m1=h*f(t(i),y(i));
m2=h*f(t(i+1),y(i)+m1);
y(i+1)=y(i)+(m1+m2)/2;
end

disp('Approximation solution by Rk2')
for i=1:n+1
fprintf('y(%f)= \t %10f \n',t(i),y(i))
end
%True solution
for i=1:n+1
y_ex(i)=3*t(i)^2-6*t(i)-5*exp(-t(i))+6;
end
  
plot(t,y,'-r*')
hold on
plot(t,y_ex,'-m')

xlabel('t','fontsize',15)
ylabel('y','fontsize',15)
legend('RK2 method','True solution')

disp('________________________________________________________________________________')
disp('t RK2 solution True solution ')
disp('________________________________________________________________________________')

for i=1:n+1
fprintf('%f \t %10f \t %15f \n',t(i),y(i), y_ex(i))
end

Approximation solution by Rk2
y(0.000000)=    1.000000
y(0.050000)=    0.951438
y(0.100000)=    0.905983
y(0.150000)=    0.864216
y(0.200000)=    0.826689
y(0.250000)=    0.793925
y(0.300000)=    0.766425
y(0.350000)=    0.744661
y(0.400000)=    0.729087
y(0.450000)=    0.720132
y(0.500000)=    0.718204
y(0.550000)=    0.723691
y(0.600000)=    0.736964
y(0.650000)=    0.758375
y(0.700000)=    0.788257
y(0.750000)=    0.826930
y(0.800000)=    0.874695
y(0.850000)=    0.931841
y(0.900000)=    0.998642
y(0.950000)=    1.075358
y(1.000000)=    1.162238
________________________________________________________________________________
t RK2 solution True solution
________________________________________________________________________________
0.000000    1.000000    1.000000
0.050000    0.951438    0.951353
0.100000    0.905983    0.905813
0.150000    0.864216    0.863960
0.200000    0.826689    0.826346
0.250000    0.793925    0.793496
0.300000    0.766425    0.765909
0.350000    0.744661    0.744060
0.400000    0.729087    0.728400
0.450000    0.720132    0.719359
0.500000    0.718204    0.717347
0.550000    0.723691    0.722751
0.600000    0.736964    0.735942
0.650000    0.758375    0.757271
0.700000    0.788257    0.787073
0.750000    0.826930    0.825667
0.800000    0.874695    0.873355
0.850000    0.931841    0.930425
0.900000    0.998642    0.997152
0.950000    1.075358    1.073795
1.000000    1.162238    1.160603
>> 1.15 RK2 method -True solution 1.05 >0.95 0.9 0.8 0.7 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0. 9 1

C)

clc;
clear all;

f=@(t,y) 3*t^2-y;
a=0;% starting point
b=1; %ending point
h=0.05; % step length
n=(b-a)/h;
y(1)=1; % Initial condition
t=a:h:b;


%Euler formula
for i=1:n
y(i+1)=y(i)+h*f(t(i),y(i));
end
y1(1)=1
for i=1:n
m1=h*f(t(i),y1(i));
m2=h*f(t(i+1),y1(i)+m1);
y1(i+1)=y1(i)+(m1+m2)/2;
end

%True solution
for i=1:n+1
y_ex(i)=3*t(i)^2-6*t(i)-5*exp(-t(i))+6;
end
  
for i=1:n+1
L(i)= abs(y(i)-y_ex(i));
end
  
for i=1:n+1
L1(i)= abs(y1(i)-y_ex(i));
end
  
plot(t,L,'-r*')
hold on
plot(t,L1,'-m')

xlabel('t','fontsize',15)
ylabel('LTE','fontsize',15)
legend('Euler','Rk2')

0.05 0.045 -Euler -Rk2 0.04 0.035 0.03 E 0.025 0.02 0.015 0.01 0.005 OL 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0. 9 1

Add a comment
Know the answer?
Add Answer to:
Complete using MatLab 1. Consider the following initial value problem 3t2-y, y(0) = 1 Using Euler's...
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