Matlab 1. Numerically solve the ODE: y\' = 10 - y 2 using obe45 . Use a start ti
ID: 3110380 • Letter: M
Question
Matlab
1. Numerically solve the ODE: y' = 10 - y2 using obe45. Use a start time of 0, end time of 1, and an initial condition of 0.
2. Using a while loop, write a function that can solve an ODE using Modified Euler's method. This function will work similarly to ode45, but the step size will have to be specified as an input.
function [tout,yout] = modifiedeuler(odefunc, tspan, y0, h)
3. Add an if statement to your your modifiedeuler function so that it doesn't overshoot the end time (i.e. change h in the final step to exactly reach the end time)
4. Adjust your modifiedeuler function so that it has adaptive time stepping. If h*f (t,y) at any point exceeds 0.5, the value for h should be halved.
Explanation / Answer
function file code:::
function dvar=chegg11(t,var)
dvar(1,1)=10-var(1)^2;
scrpit file code:::
clc
clear all
y0=[0];
tspan=[0 1];
[t,var]=ode45('chegg11',tspan,y0)
plot(t,var)
Results::
t =
0
0.0000
0.0000
0.0000
0.0000
0.0000
0.0001
0.0001
0.0001
0.0002
0.0004
0.0005
0.0006
0.0013
0.0019
0.0025
0.0031
0.0063
0.0094
0.0126
0.0157
0.0314
0.0471
0.0628
0.0785
0.1035
0.1285
0.1535
0.1785
0.2035
0.2285
0.2535
0.2785
0.3035
0.3285
0.3535
0.3785
0.4035
0.4285
0.4535
0.4785
0.5035
0.5285
0.5535
0.5785
0.6035
0.6285
0.6535
0.6785
0.7035
0.7285
0.7535
0.7785
0.8035
0.8285
0.8535
0.8785
0.9035
0.9285
0.9535
0.9785
0.9839
0.9892
0.9946
1.0000
var =
0
0.0001
0.0001
0.0002
0.0002
0.0005
0.0007
0.0010
0.0012
0.0025
0.0037
0.0050
0.0062
0.0125
0.0188
0.0251
0.0313
0.0627
0.0941
0.1255
0.1568
0.3129
0.4675
0.6198
0.7692
0.9995
1.2186
1.4247
1.6168
1.7939
1.9558
2.1027
2.2350
2.3532
2.4584
2.5514
2.6332
2.7050
2.7677
2.8223
2.8697
2.9108
2.9464
2.9770
3.0034
3.0262
3.0457
3.0625
3.0769
3.0892
3.0998
3.1089
3.1166
3.1232
3.1289
3.1338
3.1379
3.1415
3.1445
3.1471
3.1493
3.1498
3.1502
3.1506
3.1510
2.) modified Euler method(predictor -correction)
matlab code::
clc
clear all
f=@(t,y) 10-(y^2);
h=input('Enter the step lenght(h)');
t=0:h:1;
n=length(t);
i=1;
y(1)=0;
while(i<n)
k1=f(t(i),y(i));
k2=f(t(i+1),y(i)+h*k1);
k3=y(i)+(h/2)*(k1+k2);
y(i+1)=y(i)+(h/2)*(k1+k3)
i=i+1;
end
Results::
Enter the step lenght(h)0.1
y =
0 0.5475
y =
0 0.5475 1.1034
y =
0 0.5475 1.1034 1.6348
y =
0 0.5475 1.1034 1.6348 2.1122
y =
0 0.5475 1.1034 1.6348 2.1122 2.5158
y =
0 0.5475 1.1034 1.6348 2.1122 2.5158 2.8386
y =
0 0.5475 1.1034 1.6348 2.1122 2.5158 2.8386 3.0845
y =
0 0.5475 1.1034 1.6348 2.1122 2.5158 2.8386 3.0845 3.2647
y =
0 0.5475 1.1034 1.6348 2.1122 2.5158 2.8386 3.0845 3.2647 3.3928
y =
0 0.5475 1.1034 1.6348 2.1122 2.5158 2.8386 3.0845 3.2647 3.3928 3.4818