The equation is first written in a way required for RK-4 method. The equations (2) and (3) below are finally used in matlab code:
Matlab code below:
clear;
clc;
%independent variable
tfinal = 1;
h = 0.01;
nsteps = tfinal/h;
%Initial conditions
%question (a)
y0 = 10*pi/180;
z0 = 0;
x0 = 0;
yrk_sola = rk4(x0,y0,z0,nsteps,h,1);
%question (b)
y0 = 45*pi/180;
z0 = 0;
x0 = 0;
yrk_solb = rk4(x0,y0,z0,nsteps,h,1);
%question (c)
y0 = 10*pi/180;
z0 = 0;
x0 = 0;
yrk_solc = rk4(x0,y0,z0,nsteps,h,0);
%question (d)
y0 = 45*pi/180;
z0 = 0;
x0 = 0;
yrk_sold = rk4(x0,y0,z0,nsteps,h,0);
%Plotting
tspan = x0:h:tfinal;
plot(tspan,yrk_sola);
hold on
plot(tspan,yrk_solb);
hold on
plot(tspan,yrk_solc, 'LineStyle',"--");
hold on
plot(tspan,yrk_sold,'LineStyle',"--");
hold off
legend('question-(a)','question-(b)','question-(c)','question-(d)','Location','best')
title('Numerical Methods')
xlabel('time, sec')
ylabel('theta, deg')
grid on
%Function f(x,y,z)
function f = function_f(x1,y1,z1)
f = z1;
end
%Function phi(x,y,z)
function phi = function_phi(x1,y1,z1,order)
Mt = 0;
m = 1;
l = 1;
c = 0;
g = 9.81;
if order==1
phi = (Mt - c*z1 - m*g*l*y1)/(m*l*l);
else
phi = (Mt - c*z1 - m*g*l*sin(y1))/(m*l*l);
end
end
function yrk_deg = rk4(x0,y0,z0,nsteps,h,order)
%RK-4 Method
x = x0;
yoldrk = y0;
zoldrk = z0;
yrk = [y0];
zrk = [z0];
for n=1:1:nsteps
k1 = h*function_f(x,yoldrk,zoldrk);
l1 = h*function_phi(x,yoldrk,zoldrk,order);
k2 = h*function_f(x+0.5*h,yoldrk+0.5*k1, zoldrk+0.5*l1);
l2 = h*function_phi(x+0.5*h,yoldrk+0.5*k1, zoldrk+0.5*l1,order);
k3 = h*function_f(x+0.5*h,yoldrk+0.5*k2, zoldrk+0.5*l2);
l3 = h*function_phi(x+0.5*h,yoldrk+0.5*k2, zoldrk+0.5*l2,order);
k4 = h*function_f(x+h , yoldrk+k3, zoldrk+l3);
l4 = h*function_phi(x+h , yoldrk+k3, zoldrk+l3,order);
yrk(n+1) = yoldrk + (k1+2*k2+2*k3+k4)/6;
zrk(n+1) = zoldrk + (l1+2*l2+2*l3+l4)/6;
yoldrk = yrk(n+1);
zoldrk = zrk(n+1);
x= n*h;
end
yrk_deg = yrk*180/pi;
end
Output:
IMPORTANT NOTES: Using the Classical Fouth-order Runge-Kutta method to solve all the following problems, with step...
4. (25 points) Solve the following ODE using classical 4th-order Runge- Kutta method within the domain of x = 0 to x= 2 with step size h = 1: dy 3 dr=y+ 6x3 dx The initial condition is y(0) = 1. If the analytical solution of the ODE is y = 21.97x - 5.15; calculate the error between true solution and numerical solution at y(1) and y(2).
Use fourth-order Runge-Kutta method Using MATLAB Solve x - 2t = 0, (0)0,(0) = 0.1, [0, 3] by any convenient method. Graph the solution on Using MATLAB Solve x - 2t = 0, (0)0,(0) = 0.1, [0, 3] by any convenient method. Graph the solution on
Problem: Write a computer program to implement the Fourth Order Runge-Kutta method to solve the differential equation x=x2 (1) cos(x(1))-4fx(t), x(0)=-0.5 Use h-0.01. Evaluate and print a table of the solution over the interval [O, 1 x(t) 0
Need help with this MATLAB problem: Using the fourth order Runge-Kutta method (KK4 to solve a first order initial value problem NOTE: This assignment is to be completed using MATLAB, and your final results including the corresponding M- iles shonma ac Given the first order initial value problem with h-time step size (i.e. ti = to + ih), then the following formula computes an approximate solution to (): i vit), where y(ti) - true value (ezact solution), (t)-f(t, v), vto)...
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...
(e) Consider the Runge-Kutta method in solving the following first order ODE: dy First, using Taylor series expansion, we have the following approximation of y evaluated at the time step n+1 as a function of y at the time step n: where h is the size of the time step. The fourth order Runge-Kutta method assumes the following form where the following approximations can be made at various iterations: )sh+รู้: ,f(t.ta, ),. Note that the first term is evaluated at...
Solve the ordinary differential equation below over the interval 0 sts 2s using two different methods: the Euler method and the second-order Runge-Kutta method (midpoint version). Begin by writing the state space representation of the equation. Use a time step of 1 s, and place a box around the values of x and x at t- 2 s obtained using each method. Show your work. 20d's +5dr +20x = 0 dt d x(0) = 1, x'(0) = 1 Solve the...
Numerical Methods Consider the following IVP dy=0.01(70-y)(50-y), with y(0)-0 (a) [10 marks Use the Runge-Kutta method of order four to obtain an approximate solution to the ODE at the points t-0.5 and t1 with a step sizeh 0.5. b) [8 marks Find the exact solution analytically. (c) 7 marks] Use MATLAB to plot the graph of the true and approximate solutions in one figure over the interval [.201. Display graphically the true errors after each steps of calculations. Consider the...
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...
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....