Download the files myeuler.m and mymodeuler .m from the class web site. A. Creat
ID: 3841112 • Letter: D
Question
Download the files myeuler.m and mymodeuler .m from the class web site. A. Create the function program: function dy = myodeg (t, y) dy = sin (t)* cos (x); Then type the following commands: > hold on >[T Y] = myeuler (myodeg, [0, 12), . 1, 20); > plot (T, Y) Note here that ode45 can call a function program with this syntax. Position the plot window so that it can always be seen and type > [T Y] myeuler (myodeg, [0, 12], . 1, 30); > plot (T, Y) (You can use the up button to avoid typing.) Continue to increase the last number in the above until the graph stops changing (as far as you can see). Record this number and print the final graph. Type hold off and kill the plot window. B. Follow the same procedure using mymodeuler.m. C. Describe what you observed. In particular compare how fast the two methods converge as n is increased (h is decreased).Explanation / Answer
Solution:
Code:
function [T , Y] = myeuler(f,tspan,y0,n)
a = tspan(1);
b = tspan(2);
h = (b-a)/n;
% step size
t = a;
y = y0;
% t and y are the current variables
T = a;
Y = y0’;
% T and Y will record all steps
for i = 1:n y = y + h*f(t,y);
t = a + i*h;
T = [T; t];
Y = [Y; y’];
End
function [T , Y] = mymodeuler(f,tspan,y0,n)
a = tspan(1);
b = tspan(2);
h = (b-a)/n; % step size
t = a; y = y0; % t and y are the current variables
T = a; Y = y0’; % T and Y will record all steps
for i = 1:n
k1 = h*f(t,y);
k2 = h*f(t+h,y+k1);
y = y + .5*(k1+k2);
t = a + i*h;
T = [T; t];
Y = [Y; y’];
End
Function dy=myodeg(t,y)
Dy=sin(t)*cos(x);
End
Hold on
[T Y]=myeuler(@myodeg,[0,12],[1,20]);
Plot(T,Y)
[T Y]=mymodular(@myodeg,[0,12],[1,20]);
Plot(T,Y)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.