Question

Matlab & Differential Equations Help Needed

I need help with this Matlab project for differential equations. I've got 0 experience with Matlab other than a much easier project I did in another class a few semesters ago. All we've been given is this piece of paper and some sample code. I don't even know how to begin to approach this. I don't know how to use Matlab at all and I barely can do this material.

Here's the handout:

Matlab Project 2 due Monday 5.20.2019 (As with the first project, this project counts as another Quiz) For the initial value

Here's the sample code:

function [t, y] = myeuler(f, tinit, yinit, b, n)

% Euler approximation for initial value problem

% dy/dt = f(t, y), y(tinit) = yinit

% Approximations y(1),..., y(n+1) are calculated at

% the n+1 points t(1),..., t(n+1) in the interval

% [tinit, b]. The right-hand side of the differential

% equation is defined as an anonymous function f.

% Calculation of h from tinit, b, and n.

h = (b - tinit)/n;

% Initialize t and y as length n+1 column vectors.

t = zeros(n+1, 1);

y = zeros(n+1, 1);

% Calculation of points t(i) and the corresponding

% approximate values y(i) from the Euler Method formula.

t(1) = tinit;

y(1) = yinit;

for i = 1:n

t(i+1) = t(i) + h;

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

end

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


Matlab code for solving ode using Euler Improved Euler and RK4 clear all close all function for which Solution have to do funIt_euler,y euler J-ImpEuler (fun,tinit,yinit,tend, h)i y_ext-double (ySol(tend)); error abs(y_ext-y_euler end)); fprint f (\윰움움응各움움各各움움움움움움움움움움응움움움各움움움움움움움움움움움움움各움움움움움움움움움움움움움움움움움움움움各움움 %%Matlab function for Improved Euler Method function [t_imp, y_Exact solution phi(t)-t/2 exp(2*t) symbolic function inputs: t Solution Using Euler Method For h-o.0250 at t-0.50 value of y-Solution Using RK4 Method For h-0 .1000 at tー0.50 value of y2.968251. Error E-0.000031 For h-o.1000 at t-.00 value of y-7-88

%%Matlab code for solving ode using Euler Improved Euler and RK4
clear all
close all

%function for which Solution have to do
fun=@(t,y) 0.5-t+2.*y;
%initial guess
tinit=0; yinit=1;

%Answering Question 1.
syms y(t)
eqn = diff(y,t) == 0.5-t+2.*y;
cond = y(0) == yinit;
ySol(t) = dsolve(eqn,cond);

%displaying result
fprintf('Exact solution phi(t)=')
disp(ySol)

%Answering Question 2.

%all t values
tt=[0.5 1.0 1.5 2.0];
%All step size
hh=[0.025 0.0125];

fprintf('\nSolution Using Euler Method\n')
%loop for all step size and tend
for ii=1:length(hh)
    h=hh(ii);
    for jj=1:length(tt)
        tend=tt(jj);
      
        [t_euler,y_euler]=Euler(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));
      
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,t_euler(end),y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
    end
end

%Answering Question 3.

fprintf('\nSolution Using Improved Euler Method\n')
%loop for all step size and tend
for ii=1:length(hh)
    h=hh(ii);
    for jj=1:length(tt)
        tend=tt(jj);
      
        [t_euler,y_euler]=ImpEuler(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));
      
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,tend,y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
    end
end      

%Answering Question 4.

fprintf('\nSolution Using RK4 Method\n')
%All step size
hh=[0.1 0.05];
%loop for all step size and tend
for ii=1:length(hh)
    h=hh(ii);
    for jj=1:length(tt)
        tend=tt(jj);
      
        [t_euler,y_euler]=RK4(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));
      
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,tend,y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
    end
end      

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Euler Method

function [t_euler,y_euler]=Euler(f,tinit,yinit,tend,h)
    %Euler method
    % h amount of intervals
    t=tinit;         % initial t
    y=yinit;         % initial y
    t_eval=tend;     % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t_euler(1)=t;
    y_euler(1)=y;
    for i=1:n
        %Eular Steps
        m=double(f(t,y));
        t=t+h;
        y=y+h*m;
        t_euler(i+1)=t;
        y_euler(i+1)=y;
    end
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Improved Euler Method

function [t_imp,y_imp]=ImpEuler(f,tinit,yinit,tend,h)
    %Euler method
    % h amount of intervals
    t=tinit;         % initial t
    y=yinit;         % initial y
    t_eval=tend;     % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t_imp(1)=t;
    y_imp(1)=y;
  
    for i=1:n
    %improved Euler steps
       m1=double(f(t,y));
       m2=double(f((t+h),(y+h*m1)));
       y=y+double(h*((m1+m2)/2));
       t=t+h;
       y_imp(i+1)=y;
       t_imp(i+1)=t;
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Runge Kutta Method

function [t_rk,y_rk]=RK4(f,tinit,yinit,tend,h)
    %Euler method
    % h amount of intervals
    t=tinit;         % initial t
    y=yinit;         % initial y
    t_eval=tend;     % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t_rk(1)=t;
    y_rk(1)=y;
  
    for i=1:n
    %RK4 Steps
       k1=h*double(f(t,y));
       k2=h*double(f((t+h/2),(y+k1/2)));
       k3=h*double(f((t+h/2),(y+k2/2)));
       k4=h*double(f((t+h),(y+k3)));
       dy=(1/6)*(k1+2*k2+2*k3+k4);
       t=t+h;
       y=y+dy;
       t_rk(i+1)=t;
       y_rk(i+1)=y;
    end
end

%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
Matlab & Differential Equations Help Needed I need help with this Matlab project for differential equations. I've got 0 experience with Matlab other than a much easier project I did in another...
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