HW-4_EulersMethod

pdf

School

University of Michigan *

*We aren’t endorsed by this school

Course

240

Subject

Mechanical Engineering

Date

Apr 3, 2024

Type

pdf

Pages

4

Uploaded by DrSummerDolphin39

Report
Implementing Euler's Method in MATLAB The below live script can be editted and filled in to complete the MATLAB problem on Homework 4. There are only five lines below with ???'s that need to be editted for the code to run properly. %Please put your name here: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% ADD YOUR CODE HERE TO REPLACE THE ???'s %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% name = 'Geralynn McAdans' ; fprintf( 'Created by: %s' , name) Created by: Geralynn McAdans Preamble %Begin by clearing the workspace: clear; close all ; clc; %Our given variables are: t0 = 0; %Initial time at which we see the train tf = 60; %Final time to simulate to x0 = 1000; %The initial distance of the train from the station when we first spot it (in m %Setup figure for plotting to have a hold so multiple plots can be made on %the same figure figure(1) hold on Euler's Method (Numerical Approximation) Because we are going to be estimating the differential equation using a number of different step sizes, let us automate the process using a for loop. %Step size scenarios to loop through and evaluate stepSizes = [10, 5, 3, 1, 0.5]; for i = 1:length(stepSizes) %Step size (delta) to consider in loop i. delta = stepSizes(i); %Create a time vector (t) from t0 to tf with a uniform step size of delta %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% ADD YOUR CODE HERE TO REPLACE THE ???'s %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% t = 0:stepSizes(i):60; %Create a position vector (x) the same size as our time vector to be 1
%filled in while implementing Euler's method (this is known as %preallocating a vector and is good coding practice since our vector %will now not grow with each loop of j below which can be inefficient) x = zeros(size(t)); %Assign the initial position condition to the position vector (in %meters) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% ADD YOUR CODE HERE TO REPLACE THE ???'s %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% x(1) = 1000*exp (-.1); %Loop through the length of x to evaluate each position using Euler's Method for j = 2:length(x) %We start at j=2 since the initial condition was already enforced %Using the equation provided in the problem statement for Euler's %Method we can approximate x(j) as: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% ADD YOUR CODE HERE TO REPLACE THE ???'s %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% x(j) = 1000*exp(-.1*j); end %Add the step size condition to the plot plot(t, x) end Exact Solution for Reference Let us plot the exact solution as a point of reference so we can tell if your Euler method is behaving as expected t_exact = linspace(t0, tf, 1e6); %Create a time vector to evaluate the exact solution over t x_exact = 1000*exp(-0.1*t_exact); %Evaluate the exact position of the train at each time poin plot(t_exact, x_exact, 'Linewidth' , 2) %Plot x vs t for the exact solution with a line xlabel( 'Time (s)' ) %X-axis label ylabel( 'Train Distance from the Station (m)' ) %Y-axis label title( 'Euler''s Method Example' ) %Plot title hold on %Hold the plot so we can add lines to it as we %Now add a legend to your plot so we know which lines are which legendVec = [cellstr(num2str(stepSizes', 'Step Size = %0.1f' )); 'Exact Solution' ]; legend(legendVec) 2
Conclusion Do you make the train? Explain why or why not. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% ADD YOUR CODE HERE TO REPLACE THE ???'s %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% disp( 'Yes I made the train because as seen in the graph above it takes about 30 sec' ) Yes I made the train because as seen in the graph above it takes about 37 sec Export PDF Click "Export to PDF" on the top left of the screen to export this file to a PDF for submission. 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4