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
  • MATLAB HELP 3. Consider the equation y′ = y2 − 3x, where y(0) = 1.   USE THE EULER AND RUNGE-KUTTA APPROXIMATION SCRIPTS...

    MATLAB HELP 3. Consider the equation y′ = y2 − 3x, where y(0) = 1.   USE THE EULER AND RUNGE-KUTTA APPROXIMATION SCRIPTS PROVIDED IN THE PICTURES a. Use a Euler approximation with a step size of 0.25 to approximate y(2). b. Use a Runge-Kutta approximation with a step size of 0.25 to approximate y(2). c. Graph both approximation functions in the same window as a slope field for the differential equation. d. Find a formula for the actual solution (not...

  • I DESPERATELY NEED HELP WITH THIS DIFFERENTIAL EQUATIONS MATLAB ASSIGNMENT IM SUPPOSED TO BE LEARNING BUT...

    I DESPERATELY NEED HELP WITH THIS DIFFERENTIAL EQUATIONS MATLAB ASSIGNMENT IM SUPPOSED TO BE LEARNING BUT WE HAVE A SUB AND HE DIDN'T TEACH IT! ITS EULER AND IMPROVED EULER IN MATLAB! HERE IS THE LINK FOR THE IMAGE FILE THAT SHOWS THE FULL INSTRUCTIONS FOR THE CODE. https://imgur.com/a/gjmypLs Also, here is my code so far that I borrowed form an old assignment but the data is all wrong and the application of the code is slightly different so either...

  • Numerical Methods for Differential Equations - Please post full correct solution!!! - need to use MATLAB...

    Numerical Methods for Differential Equations - Please post full correct solution!!! - need to use MATLAB 3. (a) Write Matlab functions to integrate the initial value problem y = f(x,y), y(a) = yo, on an interval [a, b] using: • Euler's method • Modified Euler • Improved Euler • Runge Kutta 4 It is suggested that you implement, for example, Improved Euler as [x, y) = eulerimp('f', a, yo, b, stepsize), where (2,y) = (In, Yn) is the computed solution....

  • I mostly needed help with developing matlab code using the Euler method to create a graph. All th...

    I mostly needed help with developing matlab code using the Euler method to create a graph. All the other methods are doable once I have a proper Euler method code to refer to. 2nd order ODE of modeling a cylinder oscillating in still water wate wate Figure 1. A cylinder oscillating in still water. A cylinder floating in the water can be modeled by the second order ODE: dy dy dt dt where y is the distance from the water...

  • ///MATLAB/// Consider the differential equation over the interval [0,4] with initial condition y(0)=0. 3. Consider the...

    ///MATLAB/// Consider the differential equation over the interval [0,4] with initial condition y(0)=0. 3. Consider the differential equation n y' = (t3 - t2 -7t - 5)e over the interval [0,4 with initial condition y(0) = 0. (a) Plot the approximate solutions obtained using the methods of Euler, midpoint and the classic fourth order Runge Kutta with n 40 superimposed over the exact solution in the same figure. To plot multiple curves in the same figure, make use of the...

  • Please answer with Matlab Programming that can be copied and pasted when doing the Matlab part...

    Please answer with Matlab Programming that can be copied and pasted when doing the Matlab part 4 part 2 4. Please finish the following Matlab code for solving the ODE: dy 2(1+t) dt I.C. y(0) = 0 with the multi-step 4th order Milne's Method, and apply 4th order Runge Kutta method to the first 4 points (1 boundary point and the next 3 points). (Hint: 4th order Milne's Method Predictor: Jon = Y-3 + H25,- fia+2f1-2) h Corrector: Y:-1 =...

  • ME 32200 Programming course (MATLAB) 4. Please finish the following Matlab code for solving the ODE:...

    ME 32200 Programming course (MATLAB) 4. Please finish the following Matlab code for solving the ODE: dy = y(1+1) dt I.C. y(0) = 0 with the multi-step 4th order Milne's Method, and apply 4th order Runge Kutta method to the first 4 points (1 boundary point and the next 3 points). (Hint: 4th order Milne's Method Predictor: 7i+ = Y-3 +h(2f;- fi- +25,-2) Corrector: y = y + + +0. +45j + fi-) Where f; = f(t;,y,) and Fit =...

  • Consider the IVP, 1. Apply the FEUT to show that a solution exists. 2. Use the...

    Consider the IVP, 1. Apply the FEUT to show that a solution exists. 2. Use the Runge-Kutta method with various step-sizes to estimate the maximum t-value, t=t∗>0, for which the solution is defined on the interval [0,t∗). Include a few representative graphs with your submission, but not the lists of points. 3. Find the exact solution to the IVP and solve for t∗ analytically. How close was your approximation from the previous question? 4. The Runge-Kutta method continues to give...

  • 6. The differential equation: y 4y 2x y(0) 1/16 has the exact solution given by the...

    6. The differential equation: y 4y 2x y(0) 1/16 has the exact solution given by the following equation: v = (1 /2)s, + (14)s +1.16 Calculate y (2.0) using a step size h-0.5 using the following methods: (a) Euler (b) Euler P-c (c)4h order Runge-Kutta (d) Compare the errors for each method. (e) Solve using Matlab's ode45.m function. Include your code and a print of the solution.

  • Problem Thre: 125 points) Consider the following initial value problem: dy-2y+ t The y(0) -1 ea dt ical solution of the differential equation is: y(O)(2-2t+3e-2+1)y fr exoc the differential equat...

    Problem Thre: 125 points) Consider the following initial value problem: dy-2y+ t The y(0) -1 ea dt ical solution of the differential equation is: y(O)(2-2t+3e-2+1)y fr exoc the differential equation numerically over the interval 0 s i s 2.0 and a step size h At 0.5.A Apply the following Runge-Kutta methods for each of the step. (show your calculations) i. [0.0 0.5: Euler method ii. [0.5 1.0]: Heun method. ii. [1.0 1.5): Midpoint method. iv. [1.5 2.0): 4h RK method...

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