Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A MATLAB function M-file for Euler's method (as described in class and on pages 17-19 of the textbook) for solving the differential equation de dt
A MATLAB function M-file for Euler's method (as described in class and on pages 17-19 of the textbook) for solving the differential equation de dt TTA (this is (1.9) on page 14) is given below. As discussed in class, this numerical method is obtained by approximatingat time ,bywh ults in the computed To create a MATLAB function M-ile, ether type edit in the Command Window or select HOMENewScript or select HOME NewFunction (the latter gives you a template for creating a functio Each of these options will open a new window (an Editor window n which to type in the MATLAB statements for Euler's method. Enter the following. Statements starting with % are comunents, documenting the MATLAB code. function Euler(n,c,g,to,v0,tn,n) % print headings and initial conditions fprintf ('values of approximations v(t)n') fprintf ( , %8.3f , . tO), fprint f (-z 19 . 4f ", v0) % compute step size h h-(tn-to); % set t,v to the initial values t=to vo % compute v(t) over n time topa using Euler's method for i-1:n t-t4h fprintf (-28.3 . t) ,fprintf('%19.4f ",v) end To save your M-file, select EDITOR Save Save As At the top of this window, t should say File name: Euler.m Save as type: MATLAB files (".m) Select Save to save vour file, and close the Editor window In order to use the above function, you must specify values for the 7 local parameters in the function Euler m is the mass of the falling object c is the drag coefficient g is the gravity constant t0 is the intile, vO is the initial velocity tn is the final time at which the velocity is to be computed n is the umber of time steps into which tois divided Thus. in the function Euler. the step size h = (tn-to) is computed, and Euler's method is used to compute an approximation to the solution (t) of the differential equation at the n points (values of time to+h, to+2h, to +3h, to + 4h, , to +nh For example, in order to use Euler to solve the problem given in Example 1.2 on page 17 and to save your results in a file for printing, you could enter the following (in the MATLAB Command Window): diary filename Euler 68.1, 12.5, 9.81, 0,0.12.6) the desired results should appear here) diary off
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started