Question

In this exercise, your will be creating the function euler2 which applies Euler's method to numerically...

In this exercise, your will be creating the function euler2 which applies Euler's method to numerically solve a first order ODE, but with no overshoot.

Input variables:

ODEFUN A function representing the the equation for y'. It must be a function of t and y.
TSPAN a vector containing the start time and end time (TSPAN = [tStart,tEnd]).
Y0 The value for y at tStart.
h The step size.

Output variables:

TOUT The output time vector.
YOUT The output y vector.

Solution Process

The function should numerically solve the first order ODE y' = ODEFUN using Euler's method. Euler's method will need to be programmed with a while loop. The loop should compute the next values in TOUT and YOUT while the current value for TOUT is less than tEnd. If the simulation does overshoot, it should change the final value for h so that the simulation finishes exactly at tEnd. For example, with a start time of 0, end time of 1, and a step size of 0.3, TOUT will be [0,0.3,0.6,0.9,1].

Notes:

This is a continuation from the previous problem euler. We suggest that you simply modify your previous code to help solve this problem.

Potentially Useful Functions: while, if, end

Banned Functions: for

Function Template:

function [TOUT,YOUT] = euler2(ODEFUN,TSPAN,Y0,h)

%insert answer here

end

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

function [TOUT,YOUT] = euler2(ODEFUN,TSPAN,Y0,h)
t=TSPAN(1);
k=1;
TOUT(1)=t;
YOUT(1)=Y0;
tlim=TSPAN(2);
while t < tlim
k=k+1;
m=ODEFUN(t,Y0);
if (t+h)>TSPAN(2)
h=h-(t+h-TSPAN(2));
end
y=Y0+h*m;
  

t1=t+h;
TOUT(k)=t1;
YOUT(k)=y;
t=t1;
Y0=y;
end
  

Add a comment
Know the answer?
Add Answer to:
In this exercise, your will be creating the function euler2 which applies Euler's method to numerically...
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