Question

Using matlab solve numerically dy/dt = sin t, y(0)=0 for 0<=t<=4π the exact solution is y(t...

using matlab solve numerically dy/dt = sin t, y(0)=0 for 0<=t<=4π

the exact solution is y(t) = 1 - cos t. Compare the exact and numerical solution.

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

MATLAB CODE

% NUMERICAL METHOD : Runge-Kutta 4th order method
clear all
format short
clc;

f=@(t,y)sin(t); % y'=f(t,y)
y(1)=0; % index has been taken as i instead of 0

h=0.1;
a=0;b=4*pi; % Range of t a<=t<=b
t=a:h:b;
N=(b-a)/h;

for n=1:(length(t)-1)
k1 = h*f(t(n),y(n));
k2 = h*f(t(n)+0.5*h,y(n)+0.5*k1);
k3 = h*f((t(n)+0.5*h),(y(n)+0.5*k2));
k4 = h*f((t(n)+h),(y(n)+k3));
  
y(n+1) = y(n) + (1/6)*(k1+2*k2+2*k3+k4) ;
end
T=t;
y'; % displays all y values
%%% Exact Solution
k=1;
for t = 0:0.1:4*pi;
y_e=1-cos(t);
yext(k)=y_e;
k=k+1;
end
yext';
Comparison_t_y_ext=[T' y' yext']

Comparison_t_y_ext =

0 0 0
0.1000 0.0050 0.0050
0.2000 0.0199 0.0199
0.3000 0.0447 0.0447
0.4000 0.0789 0.0789
0.5000 0.1224 0.1224
0.6000 0.1747 0.1747
0.7000 0.2352 0.2352
0.8000 0.3033 0.3033
0.9000 0.3784 0.3784
1.0000 0.4597 0.4597
1.1000 0.5464 0.5464
1.2000 0.6376 0.6376
1.3000 0.7325 0.7325
1.4000 0.8300 0.8300
1.5000 0.9293 0.9293
1.6000 1.0292 1.0292
1.7000 1.1288 1.1288
1.8000 1.2272 1.2272
1.9000 1.3233 1.3233
2.0000 1.4161 1.4161
2.1000 1.5048 1.5048
2.2000 1.5885 1.5885
2.3000 1.6663 1.6663
2.4000 1.7374 1.7374
2.5000 1.8011 1.8011
2.6000 1.8569 1.8569
2.7000 1.9041 1.9041
2.8000 1.9422 1.9422
2.9000 1.9710 1.9710
3.0000 1.9900 1.9900
3.1000 1.9991 1.9991
3.2000 1.9983 1.9983
3.3000 1.9875 1.9875
3.4000 1.9668 1.9668
3.5000 1.9365 1.9365
3.6000 1.8968 1.8968
3.7000 1.8481 1.8481
3.8000 1.7910 1.7910
3.9000 1.7259 1.7259
4.0000 1.6536 1.6536
4.1000 1.5748 1.5748
4.2000 1.4903 1.4903
4.3000 1.4008 1.4008
4.4000 1.3073 1.3073
4.5000 1.2108 1.2108
4.6000 1.1122 1.1122
4.7000 1.0124 1.0124
4.8000 0.9125 0.9125
4.9000 0.8135 0.8135
5.0000 0.7163 0.7163
5.1000 0.6220 0.6220
5.2000 0.5315 0.5315
5.3000 0.4456 0.4456
5.4000 0.3653 0.3653
5.5000 0.2913 0.2913
5.6000 0.2244 0.2244
5.7000 0.1653 0.1653
5.8000 0.1145 0.1145
5.9000 0.0725 0.0725
6.0000 0.0398 0.0398
6.1000 0.0167 0.0167
6.2000 0.0035 0.0035
6.3000 0.0001 0.0001
6.4000 0.0068 0.0068
6.5000 0.0234 0.0234
6.6000 0.0498 0.0498
6.7000 0.0856 0.0856
6.8000 0.1306 0.1306
6.9000 0.1843 0.1843
7.0000 0.2461 0.2461
7.1000 0.3155 0.3155
7.2000 0.3916 0.3916
7.3000 0.4739 0.4739
7.4000 0.5615 0.5615
7.5000 0.6534 0.6534
7.6000 0.7487 0.7487
7.7000 0.8466 0.8466
7.8000 0.9460 0.9460
7.9000 1.0460 1.0460
8.0000 1.1455 1.1455
8.1000 1.2435 1.2435
8.2000 1.3392 1.3392
8.3000 1.4314 1.4314
8.4000 1.5193 1.5193
8.5000 1.6020 1.6020
8.6000 1.6787 1.6787
8.7000 1.7486 1.7486
8.8000 1.8111 1.8111
8.9000 1.8654 1.8654
9.0000 1.9111 1.9111
9.1000 1.9477 1.9477
9.2000 1.9748 1.9748
9.3000 1.9922 1.9922
9.4000 1.9997 1.9997
9.5000 1.9972 1.9972
9.6000 1.9847 1.9847
9.7000 1.9624 1.9624
9.8000 1.9304 1.9304
9.9000 1.8892 1.8892
10.0000 1.8391 1.8391
10.1000 1.7806 1.7806
10.2000 1.7143 1.7143
10.3000 1.6408 1.6408
10.4000 1.5610 1.5610
10.5000 1.4755 1.4755
10.6000 1.3853 1.3853
10.7000 1.2913 1.2913
10.8000 1.1943 1.1943
10.9000 1.0954 1.0954
11.0000 0.9956 0.9956
11.1000 0.8958 0.8958
11.2000 0.7970 0.7970
11.3000 0.7003 0.7003
11.4000 0.6065 0.6065
11.5000 0.5167 0.5167
11.6000 0.4317 0.4317
11.7000 0.3524 0.3524
11.8000 0.2796 0.2796
11.9000 0.2139 0.2139
12.0000 0.1561 0.1561
12.1000 0.1068 0.1068
12.2000 0.0664 0.0664
12.3000 0.0353 0.0353
12.4000 0.0138 0.0138
12.5000 0.0022 0.0022

Add a comment
Know the answer?
Add Answer to:
Using matlab solve numerically dy/dt = sin t, y(0)=0 for 0<=t<=4π the exact solution is y(t...
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