Question

Write a function M-file my_euler.m which take 5 input values, f, t_0, y_0, dt, and t_f...

Write a function M-file my_euler.m which take 5 input values, f, t_0, y_0, dt, and t_f that specify the function to be approximated, the initial t-value, the initial condition y(t0)=y0, the step-size Δt, and the final t-value at which you want to approximate the solution y(t). Your function should:

  • Use the input anonymous function f to calculate f(t,y).
  • Plot the approximate solution with green "+" symbols at the points and a red line joining them up.
  • Have a function output giving the numerical value of the approximate solution to y(t) at the final t-value, t_f.

By using this code, modify it. All variables inside my_euler.m the should be calculated from the input variables so that you can run the function from the Command Window.

t = zeros(1,8+1);

y = zeros(1,8+1);

t(1) = 1;

y(1) = 0;

dt = 0.25;

for k = 1:8

t(k+1) = t(k)+dt;

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

f = @(t,y)(2*t*exp(-1/t) +y/t^2);

end

plot(t,y,'ro-')

t(end)

y(end)

Homework Answers

Answer #1

function yn = 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 i = 2:length(y)
        y(i)=y(i-1)+dt*f(t(i-1),y(i-1));
    end
    yn=y(end);
    plot(t,y,'r-+','MarkerEdgecolor','g')
end

Know the answer?
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
Similar Questions
MATLAB Create an M-File for this IVP, dy/dt = t^2 - 16*sin(t), y(0) = 0 and...
MATLAB Create an M-File for this IVP, dy/dt = t^2 - 16*sin(t), y(0) = 0 and create an anonymous function g so that it evaluates the slope field at points of our new ODE. Ensure you use commands of using a for loop to plot the exact solution for the IVP in this exercise as well as the Euler approximations for Δt=0.5, Δt=0.25, and Δt=0.125 all on the same graph.
Consider the driven damped harmonic oscillator m(d^2x/dt^2)+b(dx/dt)+kx = F(t) with driving force F(t) = FoSin(wt). Consider...
Consider the driven damped harmonic oscillator m(d^2x/dt^2)+b(dx/dt)+kx = F(t) with driving force F(t) = FoSin(wt). Consider the overdamped case (b/2m)^2 < k/m a. Find the steady state solution. b. Find the solution with initial conditions x(0)=0, x'(0)=0. c. Use a plotting program to plot your solution for m=1, k=0.1, b=1, Fo=0.25, and w=0.5.
Using the Script: function EulerMethod1(n) X = 0 : 1/n : 1 ; Y = zeros(...
Using the Script: function EulerMethod1(n) X = 0 : 1/n : 1 ; Y = zeros( 1, n + 1 ) ; Y(1) = 1 ; for k = 1 : n m = Y(k) ; Y(k + 1) = Y(k) + m*( X(k + 1) - X(k) ) ; end clf plot( X, Y ) Create a new script, which defines the function EulerMethod2. The purpose of EulerMethod2 is to use Euler's Method to approximate the solution to the...
4. Consider the differential equation dy/dt = −ay with a > 0 an (unspecified) constant. (a)...
4. Consider the differential equation dy/dt = −ay with a > 0 an (unspecified) constant. (a) Write out the Euler step (i.e. yk as a function of yk−1) for the initial value problem y(t0) = y0, with arbitrary step size ∆t. (b) Find a number u > 0 such that if ∆t > u, then |yk| diverges to +∞ as k goes to +∞. The number u should be expressed as a function of the constant a. (c) Find another...
Assume an object with mass m=1 kg is attached to a spring with stiffness k=2 N/m...
Assume an object with mass m=1 kg is attached to a spring with stiffness k=2 N/m and lies on a surface with damping constant b= 2 kg/s. The object is subject to the external force F(t) = 4cos(t) + 2sin(t). Suppose the object starts at the equilibrium position (y(0)=0) with an initial velocity of y_1 (y'(0) = y_1). In general, when the forcing function F(t) = F*cos(γ*t) + G*sin(γ*t) where γ>0, the solution is the sum of a periodic function...
Question 3 (a) [10 marks] FAEN102 students must attend t hours, where t ∈ [0,H], of...
Question 3 (a) [10 marks] FAEN102 students must attend t hours, where t ∈ [0,H], of lectures and pass two quizzes to be in good standing for the end of semester examination. The number of students who attended between t1 and t2 hours of lectures is de- scribed by the integral ? t2 20t2 dt, 0≤t1 <t2 ≤H. t1 As a result of COVID-19, some students attended less than H2 hours of lectures before the university was closed down and...