Menu

Given Myeuler Code F T Y 1 Exp 4 T 2 Y Myeuler F 0 1 02 2 Function Yy Myeuler F T0 Y0 Dt T Q43870702

Exercise 6 1 e-4 Lets plot the exact solution y(t) = =+ = + ke-2 (with your value of k) against the Euler approximation, to

given my_euler code:

f=@(t,y) (1-exp(-4*t))-2*y;

my_euler(f,0,1,0.2,2)

function yy=my_euler(f,t_0,y_0,dt,t_f)

t=t_0:dt:t_f;

y=zeros(1,length(t));

y(1)=y_0;

for k=1:length(t)-1

y(k+1)=y(k)+dt*f(t(k),y(k));

end

plot(t,y,’+g’);

hold on;

plot(t,y,’r’);

yy=y(end);

end

Exercise 6 1 e-4 Let’s plot the exact solution y(t) = =+ = + ke-2 (with your value of k) against the Euler approximation, to check that your code works. Run your function my euler with t_0= 0, Y_0= 1, dt=0.1 and t_f = 2 Plot the exact solution on the same set of axes. Show/hide hint To plot the exact solution you will first need to create a vector of 1 values. Then you should calculate a vector of the corresponding y- values. Use the hold on command and plot function to plot over the approximation plotted with your function my_euler. Save your plot as a PNG figure and upload your PNG using the following prompt: Show/hide tip on saving as a PNG Choose file No file chosen Upload your PNG 0% Show transcribed image text Exercise 6 1 e-4 Let’s plot the exact solution y(t) = =+ = + ke-2 (with your value of k) against the Euler approximation, to check that your code works. Run your function my euler with t_0= 0, Y_0= 1, dt=0.1 and t_f = 2 Plot the exact solution on the same set of axes. Show/hide hint To plot the exact solution you will first need to create a vector of 1 values. Then you should calculate a vector of the corresponding y- values. Use the hold on command and plot function to plot over the approximation plotted with your function my_euler. Save your plot as a PNG figure and upload your PNG using the following prompt: Show/hide tip on saving as a PNG Choose file No file chosen Upload your PNG 0%

Expert Answer


Answer to given my_euler code: f=@(t,y) (1-exp(-4*t))-2*y; my_euler(f,0,1,0.2,2) function yy=my_euler(f,t_0,y_0,dt,t_f) t=t_0:dt:…

OR