Question

In: Mechanical Engineering

Write a user-defined MATLAB function that uses classical fourth order Runge-Kutta method to solve a first...

Write a user-defined MATLAB function that uses classical fourth order Runge-Kutta method to solve a first order ODE problem dydx = f(x, y) in a given interval a ? x ? b with initial condition y(a) = y0 and step size of h. For function name and arguments, use [x,y] = myrk4(f, a, b, h, y0)

Check your function to find the numerical solution for dydx=?1.2y+7e^(?0.3x) in the interval 0 ? x ? 4 with initial condition y(0)=3. Run your program three times for h=1.0, 0.1, 0.01 and plot all results along with analytical solution y=(70/9)*e^(?0.3x)?(43/9)*e^(?1.2x) on the same figure.

Solutions

Expert Solution

function [X,Y] = myrk4(f,a,b,h,y0)
syms x y
X = [a:h:b];
Y(1) = y0;
for i=1:size(X,2)-1
k1 = h*subs(f,{x,y},{X(i),Y(i)});
k2 = h*subs(f,{x,y},{X(i)+h/2,Y(i)+k1/2});
k3 = h*subs(f,{x,y},{X(i)+h/2,Y(i)+k2/2});
k4 = h*subs(f,{x,y},{X(i)+h,Y(i)+k3});
Y(i+1) = Y(i)+k1/6+k2/3+k3/3+k4/6;
end

clear
a = 0;
b = 4;
y0 = 3;
h = 1;
syms x y
f = -1.2*y+7*exp(-0.3*x);
Y_exact =@(x) (70/9).*exp(-0.3*x)-(43/9).*exp(-1.2*x);
figure('position',[0 0 800 300]);
hold on;
i=1;
for h=[1,0.1,0.01]
[X,Y] = myrk4(f,a,b,h,y0);
subplot(1,3,i)
plot(X,Y,X,Y_exact(X))
xlabel('X')
ylabel('Y')
title(['h=']+string(h))
legend('numerical','analytical')
i=i+1;
end


Related Solutions

With the aid of fourth order Runge-Kutta method, solve the competing species model defined by dx/dt...
With the aid of fourth order Runge-Kutta method, solve the competing species model defined by dx/dt =x(2 − 0.4x − 0.3y), x(0) = 2 dy/dt =y(1 − 0.1y − 0.3x), y(0) = 4 where the populations x(t) and y(t) are measured in thousands and t in years. Use a step size of 0.2 for 0 ≤ t ≤ 2 and plot the trajectories of the populations with Matlab or GNU Octave.
Use 3 steps of the Runge-Kutta (fourth order) method to solve the following differential equation to...
Use 3 steps of the Runge-Kutta (fourth order) method to solve the following differential equation to t = 2.4, given that y(0) = 2.3. In your working section, you must provide full working for the first step. To make calculations easier, round the tabulated value of y at each step to four decimal places. a) Provide the four K-values that are calculated at the first step, with four decimal places. b) Provide your answer for y(2.4) with four decimal places....
Q 4. With the aid of fourth order Runge-Kutta method, solve the competing species model [20...
Q 4. With the aid of fourth order Runge-Kutta method, solve the competing species model [20 points] defined by dx =x(2 − 0.4x − 0.3y), x(0) = 4 dt dy =y(1 − 0.1y − 0.3x), y(0) = 3 dt where the populations x(t) and y(t) are measured in thousands and t in years. Use a step size of 0.2 for 0 ≤ t ≤ 2 and plot the trajectories of the populations with Matlab or GNU Octave.
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
1)Select all that applies to the Fourth-order Runge-Kutta (RK4) method K subscript 1 equals f left...
1)Select all that applies to the Fourth-order Runge-Kutta (RK4) method K subscript 1 equals f left parenthesis t subscript k comma y subscript k right parenthesis K subscript 2 equals f left parenthesis t subscript k plus h over 2 comma space y subscript k plus h over 2 space K subscript 1 right parenthesis K subscript 3 equals f left parenthesis t subscript k plus h over 2 comma space y subscript k plus h over 2 space K...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
Use Classic Runge-Kutta method with h = 1 to solve the system y” - y’ -...
Use Classic Runge-Kutta method with h = 1 to solve the system y” - y’ - 6y = 0, y(0) = 2, y’(0) = 3 on [0,1]
Problem Four (12 Marks) Use Runge Kutta method of order four to approximate the solution of...
Problem Four Use Runge Kutta method of order four to approximate the solution of the initial value problem ?′ + 2? = ??3?, 0 ≤ ? ≤ 1, ?(0) = 0, ???ℎ ℎ = 0.5 Hint: Compute ?(0.5) ??? ?(1)
Using Runge-Kutta method of order 4 to approximate y(1) with step size h = 0.1 and...
Using Runge-Kutta method of order 4 to approximate y(1) with step size h = 0.1 and h = 0.2 respectively (keep 8 decimals): dy/dx = x + arctan y, y(0) = 0. Solutions: when h = 0.1, y(1) = 0.70398191. when h = 0.2, y(1) = 0.70394257.
MATLAB Write a user defined function for a projectile motion. If a ball is launched from...
MATLAB Write a user defined function for a projectile motion. If a ball is launched from initial position(0,0) with a velocity v0 at angle θ, determine your horizontal and vertical position. Please plot x vs. t, y vs. t and y vs. x.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT