Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

function [t_vec,Y] = ForwardEuler(f,t_max,h,y_1_vec) %f is any MATLAB function handle %t_max: max t to solve to %h: delta t %y_1_vec: init cond. t_vec = 0:h:t_max;

function [t_vec,Y] = ForwardEuler(f,t_max,h,y_1_vec) %f is any MATLAB function handle %t_max: max t to solve to %h: delta t %y_1_vec: init cond. t_vec = 0:h:t_max; N = length(t_vec); %maximum number of iterations Y = zeros(N, length(y_1_vec)); Y(1,:) = y_1_vec; for i=2:N %do each step of Forward Euler Y(i,:) = Y(i-1,:) + h*f(t_vec(i-1), Y(i-1,:)); end f = @(t,y) [y(1)*y(2) , t*y(2)^2]; t_max = 5; h = 1/10; y_1_vec = [0.1, 0.5]; %use only for 2-dim vectors y as input [t_vec,Y] = ForwardEuler(f,t_max,h,y_1_vec); t_vec Y %plot(t_vec,Y(:,1),'ro-',t_vec,Y(:,2),'bo-'); %xlabel('t'); plot(Y(:,1), Y(:,2), 'go-'); f = @(t,y) y; t_max = 1; h = 1/100; y_1_vec = 1; %use only for 2-dim vectors y as input [t_vec,Y] = ForwardEuler(f,t_max,h,y_1_vec); Y exact_y_vec = exp(t_vec); plot(t_vec,Y,'ro-',t_vec,exact_y_vec,'b-') legend('approx. sol','exact sol.') Question 2 Plot for h = 0.1 Plot for h = 0.01 Question 3 The Error increases with increasing value of hm Question 4 function [ ] = ForwardEuler(f,t_max,h,y_1_vec) clc clear t_max = 5; % max t to solve to h = 0.01; % delta t y_1_vec = 1/2; % init cond. t_vec = 0:h:t_max; N = length(t_vec); % maximum number of iterations Y = zeros(N, length(y_1_vec)); Y(1,:) = y_1_vec; f =@(t,y)(y*(1-y)); for i = 2:N % do each step of Forward Euler Y(i,:) = Y(i-1,:) + h*f(t_vec(i-1), Y(i-1,:)); end % Exact solution Y_exact = 1./(1 + exp(-t_vec)); % Making t_vec a column vector t_vec = t_vec'; disp('t_vec Y Y_exact'); for i = 1:N % Displaying the values of t_vec and Y as a column vector fprintf('%f %f %f\ ',t_vec(i),Y(i),Y_exact(i)) end plot(t_vec,Y,'r-',t_vec,Y_exact,'b') title('Solution to diff(y)=y(1-y) fro h = 0.01') xlabel('t_ vec') ylabel('Y') legend('Aproximate','Exact') end function [ ] = ForwardEuler(f,t_max,h,y_1_vec) clc clear t_max = 5; % max t to solve to h = 0.1; % delta t y_1_vec = 1/2; % init cond. t_vec = 0:h:t_max; N = length(t_vec); % maximum number of iterations Y = zeros(N, length(y_1_vec)); Y(1,:) = y_1_vec; f =@(t,y)(y*(1-y)); for i = 2:N % do each step of Forward Euler Y(i,:) = Y(i-1,:) + h*f(t_vec(i-1), Y(i-1,:)); end % Exact solution Y_exact = 1./(1 + exp(-t_vec)); % Making t_vec a column vector t_vec = t_vec'; disp('t_vec Y Y_exact'); for i = 1:N % Displaying the values of t_vec and Y as a column vector fprintf('%f %f %f\ ',t_vec(i),Y(i),Y_exact(i)) end plot(t_vec,Y,'r-',t_vec,Y_exact,'b') title('Solution to diff(y)=y(1-y) fro h = 0.1') xlabel('t_ vec') ylabel('Y') legend('Aproximate','Exact') end function [ ] = ForwardEuler(f,t_max,hm,y_1_vec) clc clear t_max = 5; % max t to solve to y_1_vec = 1/2; % init cond. for m = 1:1:8 hm = 1/(2^m); % delta t t_vec = 0:hm:t_max; N = length(t_vec); % maximum number of iterations Y = zeros(N, length(y_1_vec)); Y(1,:) = y_1_vec; f =@(t,y)(y*(1-y)); for i = 2:N % do each step of Forward Euler Y(i,:) = Y(i-1,:) + hm.*f(t_vec(i-1), Y(i-1,:)); end % Exact solution Y_exact = 1./(1 + exp(-t_vec)); Em(m) = max(abs(Y-Y_exact')); end hm = [1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256]; Em = Em'; plot(hm,Em) title('Plot of Error against hm') xlabel('hm') ylabel('Error, Em') end

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

Linear Algebra and Its Applications

Authors: David C. Lay

4th edition

321791541, 978-0321388834, 978-0321791542

More Books

Students also viewed these Mathematics questions