I want to make two plots in one figure in matlab of the linear and the nonlinear
ID: 3216473 • Letter: I
Question
I want to make two plots in one figure in matlab of the linear and the nonlinear differential equation of the pendulum
clear; clc;
L = 2; g=32.17;
F=@(t,Y) [ Y(2) ; ...
-(g/L)*sin(Y(1))];
H=@(t,Y) [ Y(2) ; ...
-(g/L)*(Y(1))];
t0=0; tf=2; n=1000; h=(tf-t0)/n;
alpha = [ pi/6 ; 0 ];
[t,w] = midpoint(t0,h,n,alpha,F);
[t',w']
plot(t,w(1,:));grid on
function [t,w] = midpoint(t0,h,n,alpha,F)
w(1:2,1)=alpha; t(1)=t0;
for i=1:n
K1 = F(t(i),w(1:2, i));
K2 = F(t(i)+0.5*h,w(1:2, i)+0.5*h*K1);
w(1:2, i+1)=w(1:2, i)+(K2)*h;
t(i+1)=t(i)+h;
end
end
Explanation / Answer
you can merge two plots in matlab by the following way- x = 0:0.1:10; y1 = %graph1% ; y2 = %graph2% ; figure subplot(2,1,1) % add first plot in 2 x 1 grid plot(x,y1) title('Subplot 1') subplot(2,1,2) % add second plot in 2 x 1 grid plot(x,y2,'+') % plot using + markers title('Subplot 2')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.