Question
use matlab

Assignment: 1) Write a function program that implements the 4th Order Runge Kutta Method. The program must plot each of the k
0 0
Add a comment Improve this question Transcribed image text
Answer #1


88 Matlab code using RK4 for 2nd order differential equation clear all close all tinitial condition t_in=2; t_end=5; functionplot(t, K4) xlabel(t) ylabel(64) subplot(5,1,5) plot(t,y1) xlabel(t) ylabel(y(t)) end $$$$$$$$$$$$$$$$$$ End of Code

%%Matlab code using RK4 for 2nd order differential equation
clear all
close all
%initial condition
t_in=2; t_end=5;
%function
f=@(t,y) -0.5.*y+exp(-t);

%initial guess
y1_in=4.143883;
h=0.5;
[y1,K1,K2,K3,K4,tt]=RK4(f,h,y1_in,t_in,t_end);

%function for RK4 method
function [y1,K1,K2,K3,K4,t]=RK4(f,h,y1_in,t_in,t_end)
  
    %initial conditions
    t(1)=t_in;y1(1)=y1_in;
         
    t_in=t(1);            %Initial t
    t_max=t_end;          %Final t
    n=(t_max-t_in)/h; %number of steps
    %Runge Kutta 4 iterations
    for i=1:n+1

        k0=h*f(t(i),y1(i));
        K1(i)=k0;
        k1=h*f(t(i)+(1/2)*h,y1(i)+(1/2)*k0);
        K2(i)=k1;
        k2=h*f(t(i)+(1/2)*h,y1(i)+(1/2)*k1);
        K3(i)=k2;
        k3=h*f(t(i)+h,y1(i)+k2);
        K4(i)=k3;
        if i<=n
            t(i+1)=t_in+i*h;
            y1(i+1)=double(y1(i)+(1/6)*(k0+2*k1+2*k2+k3));
        end
    end
    figure(1)
     subplot(5,1,1)
    plot(t,K1)
    xlabel('t')
    ylabel('K1')
     subplot(5,1,2)
    plot(t,K2)
    xlabel('t')
    ylabel('K2')
    subplot(5,1,3)
    plot(t,K3)
    xlabel('t')
    ylabel('K3')
    subplot(5,1,4)
    plot(t,K4)
    xlabel('t')
    ylabel('K4')
    subplot(5,1,5)
    plot(t,y1)
    xlabel('t')
    ylabel('y(t)')
  
end

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

Add a comment
Know the answer?
Add Answer to:
use matlab Assignment: 1) Write a function program that implements the 4th Order Runge Kutta Method....
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
  • Implement the 4th order Runge-Kutta algotithm in MATLAB. Use the script you produced to integrate...

    Implement the 4th order Runge-Kutta algotithm in MATLAB. Use the script you produced to integrate the following function x(t)--10t + e-t , x(0)--1; t.-0 t, = 1 Vary At and observe the difference in your results. Let At 0.2 sec., 0.1 sec., 0.05 sec. and 0.01 sec. Now integrate the function analytically and compare your result with the results obtained numerically Implement the 4th order Runge-Kutta algotithm in MATLAB. Use the script you produced to integrate the following function x(t)--10t...

  • Runge-Kutta method R-K method is given by the following algorithm. o y(xo)- given k2-f(xi5.yi tk,...

    Runge-Kutta method R-K method is given by the following algorithm. Yo = y(xo) = given. k1-f(xy) k4-f(xi +h,yi + k3) 6 For i = 0, 1, 2, , n, where h = (b-a)/n. Consider the same IVP given in problem 2 and answer the following a) Write a MATLAB script file to find y(2) using h = 0.1 and call the file odeRK 19.m b) Generate the following table now using both ode Euler and odeRK19 only for h -0.01....

  • Use the Runge Kutta 4th Order (RK-4) Method on the function below to predict the value...

    Use the Runge Kutta 4th Order (RK-4) Method on the function below to predict the value of y(0.1), given t = 0, y(0)-2, and h-01. Report your answer to 3 decimal places. dy/dt = e + 3y Answer: Use the Runge-Kutta 4th Order (RK-4) Method on the function below to predict the value of y(0.2), given y(0.1) from the previous question, and h = 0.1. Report your answer to 3 decimal places. -t dy/dt -e +3y Answer

  • Can you help me with this problem? This has to be done using Matlab and solving with runge-katta, euler method, and the built in function ode45

    Consider a cylindrical storage tank with surface area A which contains a liquid at depth y:At time t = 0, the tank is empty (y = 0 when t = 0). Liquid is supplied to the tank at a sinusoidal rate Qin =3Qsin2 (t) and withdrawn from the tank as: π‘„π‘œπ‘’π‘‘ = 3(𝑦 βˆ’ π‘¦π‘œπ‘’π‘‘) 1.5 if 𝑦 > π‘¦π‘œπ‘’π‘‘ π‘„π‘œπ‘’π‘‘ = 0 if 𝑦 ≀ π‘¦π‘œπ‘’π‘‘Β Please note that both 𝑄𝑖𝑛 and π‘„π‘œπ‘’π‘‘ have units m3 /h.Β The mass...

  • Solve Example 6.2 on page 111 of textbook, using (1) the 4th order Runge Kutta method...

    Solve Example 6.2 on page 111 of textbook, using (1) the 4th order Runge Kutta method and (2) Simulink method, respectively. Please submit your MATLAB code m file and slx file You can use the results from the code attached below (from the textbook by ODE45 solver) as a reference Example 6.2 ode45 ex.m % This program solves a system of 3 differential equations % by using ode45 function % y1 (0)=0, y2 (0)=1.0, y3 (0)=1.0 clear; clc initial [0.0...

  • help me with this. Im done with task 1 and on the way to do task...

    help me with this. Im done with task 1 and on the way to do task 2. but I don't know how to do it. I attach 2 file function of rksys and ode45 ( the first is rksys and second is ode 45) . thank for your help Consider the spring-mass damper that can be used to model many dynamic systems -- ----- ------- m Applying Newton's Second Law to a free-body diagram of the mass m yields the...

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