Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a numerical differential equation problem called Euler's method. My function is working for a single step size (value h) but I am trying

I have a numerical differential equation problem called Euler's method. My function is working for a single step size (value h) but I am trying to change the code to allow me to loop over 5 different values h (by changing h from a single value to a list of possible values).

I would like to change h to h=[0.2,0.1,0.05,0.0025,0.0125] and have the values to then create the plot showing the analytical solution and the Euler's method solutions at different values of the step size.

My code that is working for a single value of the step size h is:

So the question is really trying to create Euler's method for different step sizes i.e. "How does one change our function to loop over a list and plot the results with matlab"?

clear; clc; close('all');

y0 = 1;

t0 = 0;

h = 0.2;

tn = 5; % equal to: t0 + h*n, with n the number of steps

[t, y] = Euler(t0, y0, h, tn);

plot(t, y, 'b');

% exact solution y=e^(-1 + e^(-t))):

tt = (t0:0.001:tn);

yy = exp(-1+exp(-tt));

hold('on');

plot(tt, yy, 'r');

hold('off');

legend('Euler', 'Exact');

function [t, y] = Euler(t0, y0, h, tn)

fprintf('%10s%10s%10s%15s ', 'i', 'yi', 'ti', 'f(yi,ti)');

fprintf('%10d%+10.2f%+10.2f%+15.2f ', 0, y0, t0, f(y0, t0));

t = (t0:h:tn)';

y = zeros(size(t));

y(1) = y0;

for i = 1:1:length(t) - 1

y(i + 1) = y(i) + h * f(y(i), t(i));

fprintf('%10d%+10.2f%+10.2f%+15.2f ', i, y(i + 1), t(i + 1), f(y(i + 1), t(i + 1)));

end

end

% in this case, f(y,t) = f(y)

function dydt = f(y, t)

dydt = -exp(-t)*y;

end

image text in transcribed

Find the true solution of the IVP: y'(t) -e y(t), y(0) = 1. Then write a single MATLAB code in a file named Prob3.m (also upload to Canvas) to preform the following tasks: (a) Using the forward Euler method provided in class, approximate the solution to this problem numerically with stepsizes h = {0.2, 0.1, 0.05, 0.025, 0.0125} for t (0,5). Output the solution and absolute error every 1 unit of time, as well as the overall maximum of these errors for the time interval. (b) Store each of these maximum errors in a vector of the same size as your vector of h values. (c) Using your stored error values, numerically estimate the order of convergence for Euler's method on this problem. (d) Plot numerical solutions vs. the true solution for all h above in the same figure). Find the true solution of the IVP: y'(t) -e y(t), y(0) = 1. Then write a single MATLAB code in a file named Prob3.m (also upload to Canvas) to preform the following tasks: (a) Using the forward Euler method provided in class, approximate the solution to this problem numerically with stepsizes h = {0.2, 0.1, 0.05, 0.025, 0.0125} for t (0,5). Output the solution and absolute error every 1 unit of time, as well as the overall maximum of these errors for the time interval. (b) Store each of these maximum errors in a vector of the same size as your vector of h values. (c) Using your stored error values, numerically estimate the order of convergence for Euler's method on this problem. (d) Plot numerical solutions vs. the true solution for all h above in the same figure)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions