Question

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 1.0 1.0] tspan 0.0:0.1:10.0; options-odeset (RelTol,1.0e-6, AbsTol, [1.0e-6 1.0e-6 1.0e-6]) [t, Y] ode45 (@dydt3,tspan, initial,options) disp (P) y1 P(,2) y2-P(:,3) y3 P:,4) fid-fopen (output.txt,w) fprintf (fid, y (2) y (3) n) for i=1 :2 : 101 fprintf (fid, %6.2f %10.2f %10.2f 10.2 \ n end fclose (fid)i plot (tl, Y(),t1,Y(, 2),-.,ti,Y:,3),), xlabelt, ylabel (Y(1),Y (2), Y (3)),titleY vs. t grid, text (6.o, -1.2, y (1), text (7.7, -0.25, y (2), text (4.2,0.85, y (3) % dydt3.m % functions for example problem function Yprime-dydt3 (t, Y) Yprime-zeros (3,1) Yprime (1)-Y (2) Y (3) t; yprime (2)--Y(1)*Y (3); Yprime (3)-0.51*Y (1) Y (2)

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

t=(ti:h:tf);
n=length(t);
if t(n)<tf
t(n+1)=tf;
n=n+1;
end
else t=tspan;
end

while(1)
tend=t(np+1);
hh=t(np+1)-t(np);
while(1)
if hh>h,hh=h;
end
while(1)
if tt+hh>tend,hh=tend-tt;
end
k1=dydt(tt,y(i,:),varargin{:})';
ymid=y(1,:)+k1.*hh./2;
k2=dydt(tt+hh/2,ymid,varargin{:})';
ymid=y(i,:)+k2*hh/2;
k3=dydt(tt+hh/2,ymid,varargin{:});
yend=y(i,:);
k4=dydt(tt+hh,yend,varargin{:})';
phi=(k1+2*(k2+k3)+k4)/6;
y(i+1,:)=y(i,:)+phi*hh;
tt=tt+hh;
i=i+1;
if tt>=tend,break,end
end
np=np+1;
tp(np)=tt;
yp(np,:)=y(i,:);
if tt>=tf,break,end
end
plot(tp,yp)
end

clc;
clear all;

h=1.5;   
x = 0:h:3;   
y = zeros(1,length(x));
y(1) = 5;   
F_xy = @(t,r) 3.*exp(-t)-0.4*r;

for i=1:(length(x)-1)
k_1 = F_xy(x(i),y(i));
k_2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = F_xy((x(i)+h),(y(i)+k_3*h));

y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h;  
end


function [t,y] = System(f,n,h)
M = [0,1;-3,-4];
y(1, :) = [2, -4];
f =@(t,y)(M*y')';
t(1) = 0;


for j = 1 : n
k1 = h * f(t(j), y(j,:) );
k2 = h * f( t(j) + h/2, y(j,:) + k1/2 );
k3 = h * f( t(j) + h/2, y(j,:) + k2/2 );
k4 = h * f( t(j) + h, y(j,:) + k3 );
y(j+1,:) = y(j,:) + (k1 + 2*k2 + 2*k3 + k4) / 6;
end

clc; % Clears the screen
clear all;

thete=30;
g= 9.81; %Constant

h=0.01; % step size
tfinal = 3;
N=ceil(tfinal/h); %ceil is round up   

t(1) = 0;% initial condition
Vx(1)=0;%initial accleration
X(1)=0;
Vz(1)=0;
Z(1)=20;%initial velocity
fn = 0.866;
Bz = 0.35;
Phi(1)= 30;
W(1)= 0;

%Bxx = ((Z-Z(1))+5.8*cos(thete)+Bz*(sin(Phi)-sin(thete)));
Bxx = 0.5;
%sliding phase
F_X = @(t,X,Vx,Phi) Vx;
F_Z = @(t,Z,Vz,Phi) Vz;
F_Phi= @(t,Phi,W) W;
F_Vx = @(t,X,Vx,Phi)(fn*(cos(Phi)-0.05*(sin(Phi))));
F_Vz = @(t,Z,Vz,Phi)(fn*(sin(Phi)+0.05*(cos(Phi)))-g);
F_W = @(t,Phi,W)((fn*(Bxx+Bz*0.05))/20000);

for i=1:N; % calculation loop

%update time
t(i+1)=t(i)+h;
%update motions main equation
%rot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= F_Phi (t(i)+0.5*h,Phi(i)+0.5*h*k_5,W(i)+0.5*h*L_5);
L_6 = F_W (t(i)+0.5*h,Phi(i)+0.5*h*k_5,W(i)+0.5*h*L_5);
k_7 = F_Phi (t(i)+0.5*h,Phi(i)+0.5*h*k_6,W(i)+0.5*h*L_6);
L_7 = F_W (t(i)+0.5*h,Phi(i)+0.5*h*k_6,W(i)+0.5*h*L_6);
k_8 = F_Phi (t(i)+ h,Phi(i)+ h*k_7,W(i)+ h*L_7);
L_8 = F_W (t(i)+ h,Phi(i)+ h*k_7,W(i)+ h*L_7);

X(i+1) = X(i) + (h/6)*(k_1+2*k_2+2*k_3+k_4);
Vx(i+1) = Vx(i) + (h/6)*(L_1+2*L_2+2*L_3+L_4);
Z(i+1) = Z(i) + (h/6)*(k_11+2*k_22+2*k_33+k_44);
Vz(i+1) = Vz(i) + (h/6)*(L_11+2*L_22+2*L_33+L_44);
Phi(i+1)= Phi(i) + (h/6)*(k_5+2*k_6+2*k_7+k_8);
W(i+1) = W(i) + (h/6)*(L_5+2*L_6+2*L_7+L_8);


end

figure
plot(X,Z,'--', 'Linewidth', 1.5, 'color', 'red')
xlabel('time')
ylabel('height')
legend('position')
figure
plot(Phi,W,'--', 'Linewidth', 1.5, 'color', 'blue')
xlabel('time')
ylabel('height')
legend('rotation')
figure
plot(t,Vx,'--', 'Linewidth', 1.5, 'color', 'black')
hold on
plot(t,Vz,'--', 'Linewidth', 1.5, 'color', 'yellow')
hold on
plot(t,W,'--','Linewidth',1.5,'color','green')
xlabel('time')
ylabel('height')
legend('Vx','Vz','W')

fprintf('time %.3f\n x %.3f\n z %.3f\n ',t,X,Z);
fprintf('W %.3f\n',W);
fprintf('Phi %.3f\n',Phi);

function RKSystems(a, b, m, N, alpha, f)
h = (b - a)/N;
t = a;
w = zeros(1, m);

for j = 1:m
w(j) = alpha(j);
end

fprintf('t = %.2f;', t);
for i = 1:m
fprintf(' w(%d) = %.10f;', i, w(i));
end
fprintf('\n');

k = zeros(4, m);
for i = 1:N
for j = 1:m
k(1, j) = h*f{j}(t, w);
end

for j = 1:m
k(2, j) = h*f{j}(t + h/2, w + (1/2)*k(1));
end

for j = 1:m
k(3, j) = h*f{j}(t + h/2, w + (1/2)*k(2));
end

for j = 1:m
k(4, j) = h*f{j}(t + h, w + k(3));
end

for j = 1:m
w(j) = w(j) + (k(1, j) + 2*k(2, j) + 2*k(3, j) + k(4, j))/6;
end

t = a + i*h;

fprintf('t = %.2f;', t);
for k = 1:m
fprintf(' w(%d) = %.10f;', k, w(k));
end
fprintf('\n');

end
end

Add a comment
Know the answer?
Add Answer to:
Solve Example 6.2 on page 111 of textbook, using (1) 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
  • Using the fourth order Runge-Kutta method (KK4 to solve a first order initial value problem NOTE:...

    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)...

  • 3) Recall the Hardy-Weinberg problem described in your text (page 273-274). The multinomial distribution for random...

    3) Recall the Hardy-Weinberg problem described in your text (page 273-274). The multinomial distribution for random variables Yı, Y2, Y3 (can extend to more than 3) is given by n! P(Yı y1, Y2 = y2, Y3 = ya) Ул!ур!у! Рі Р2 р. where y + y3 = n and the parameters pi,P2, P3 are subject to the constraint p1 +p2 +p3 = 1. This distribution is an extension of the binomial distribution. In fact, the distribution of each Y, i=...

  • 4. (25 points) Solve the following ODE using classical 4th-order Runge- Kutta method within the domain...

    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).

  • MATLAB code for a double pendulum. Please explain each lines for these codes pls. -------------------------------------...

    MATLAB code for a double pendulum. Please explain each lines for these codes pls. ---------------------------------------------------------------------------- clc close all clear all %---------Parameters------------------------------------------------------ L1=1; L2=1 ; M_1=2 ; M_2=1; G=9.8; %---------initial condition----------------------------------------------- tspan=30; theta1=3; theta1_prime=0; theta2=2.5; theta2_prime=0; y0=[theta1 theta1_prime theta2 theta2_prime]; [t,y]=ode45(@pend, [0 ,tspan],[ 3 0 2 0]); %---position of mass 1 and mass 2---------------------------------------- x1=L1*sin(y(:,1)); y1=-L1*cos(y(:,1)); x2=L1*sin(y(:,1))+l2*sin(y(:,3)); y2=-L1*cos(y(:,1))-l2*cos(y(:,3)); %------visualizing the result--------------------------------------------- figure(1) plot(x1,y1,'linewidth',2) hold on plot(x2,y2,'r','linewidth',2) h=gca; get(h,'fontSize') set(h,'fontSize',14) xlabel('X','fontSize',14); ylabel('Y','fontSize',14); title('Chaotic Double Pendulum','fontsize',14) fh = figure(1); set(fh, 'color', 'white'); figure(2)...

  • How can I make these equations into 6 first order equations to input them in MATLAB...

    How can I make these equations into 6 first order equations to input them in MATLAB as : function yp = ivpsys_fun_oscillator(t, y) % IVPSYS_FUN evaluates the right-hand-side of the ODE's. % Inputs are current time and current values of y. Outputs are values of y'. % Call format: yp = ivpsys_fun_oscillator(t, y) global m k l %% Define ODE for IVP % Reduce high order derivative to first order % y1 -> x1, y2 -> x2, y3 -> x3...

  • 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...

  • please solve example 3 The Definition of the Gauss Map and Its Fundamental Properties 141 Example...

    please solve example 3 The Definition of the Gauss Map and Its Fundamental Properties 141 Example 3. Consider the cylinder {(x, y, z) € R’; x2 + y2 = 1). By an gument similar to that of the previous example, we see that N = (x, y,0) and N=(x, y,0) are unit normal vectors at (x, y, z). We fix an orienta- tion by choosing N = (-x, -y, 0) as the normal vector field. By considering a curve (x(t),...

  • I'm trying to solve this differential equations by using matlab and I've got a plot from the code attached. But...

    I'm trying to solve this differential equations by using matlab and I've got a plot from the code attached. But I wanna get a plot of completely sinusoidal form. If I can magnify the plot and expand x-axis, maybe we can get the sinusoidal form. So help me with this problem by using matlab. Example is attached in below. One is the plot from this code and another is example. function second_order_ode2 t=[0:0.001:1]; initial_x=0; initial_dxdt=0; [t,x]=ode45(@rhs,t,[initial_x initial_dxdt]); plot(t,x(:,1)) xlabel('t') ylabel('x')...

  • I'm trying to solve this differential equations by using matlab and I've got a plot from the code attached. But...

    I'm trying to solve this differential equations by using matlab and I've got a plot from the code attached. But I wanna get a plot of completely sinusoidal form. If I can magnify the plot and expand x-axis, maybe we can get the sinusoidal form. So help me with this problem by using matlab. Example is attached in below. One is the plot from this code and another is example. function second_order_ode2 t=[0:0.001:1]; initial_x=0; initial_dxdt=0; [t,x]=ode45(@rhs,t,[initial_x initial_dxdt]); plot(t,x(:,1)) xlabel('t') ylabel('x')...

  • (1) (2) (3) (4) (1 point) 18 28 -1 (Note: This problem has several parts. The latter parts will not appear until afte...

    (1) (2) (3) (4) (1 point) 18 28 -1 (Note: This problem has several parts. The latter parts will not appear until after the earlier parts are completed correctly.) This is similar to a problem in the textbook. A graph of f is shown above. Estimate the following integral. 18 f(x)dx Notice the integral points x-values y-values Estimate the integral Sf(x)dx x-values If we use these integral points to divide the region into five intervals, what are the x-values (including...

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