Kindly find error in the code

7 visualizzazioni (ultimi 30 giorni)
Mrudul Agrawal
Mrudul Agrawal il 1 Nov 2020
Commentato: Star Strider il 2 Nov 2020
Hey all!!
I have to 2 doubly differential dependant equations using ode45 function. My code is giving me values for 1st depensant variable but not giving any value for 2nd one. Kindly help me fix my code
here is my code:
tRange = [0,10]
g = 9.8
l = 1
k = 0.00006
%x1 = diff(x,t)
[tsol, ans1] = ode45(@pend,tRange,[0.05;0;0;0])
function dYdt = pend(t,Y)
% extracting variables from Y column vaector
x = Y(1)
x1 = Y(2)
y = Y(3)
y1 = Y(4)
% some constants defined
g = 9.8
l = 1
k = 0.000051
% Four differential eqns because 2 doubly differential equations.
dxdt = x1
dx1dt = -(g/l)*x + k*y1
dydt = y1
dy1dt = -(g/l)*y - k*x1
% arranging all in column vector
dYdt = [dxdt;dx1dt;dydt;dy1dt]
end

Risposta accettata

Star Strider
Star Strider il 1 Nov 2020
Your code runs for me without error. Note that the magnitudes of columns 3 & 4 are about 1/1000 the amplitude fo columns 1 & 2, so they plot essentially as straight lines near 0. Multiplying them by 1000 will allow you to see their structure:
figure
plot(tsol, ans1.*[1 1 1000 1000])
grid
.
  2 Commenti
Mrudul Agrawal
Mrudul Agrawal il 2 Nov 2020
is there any autoscale option in plot fxn?
Star Strider
Star Strider il 2 Nov 2020
There is no autoscale. The best option in that situation would be yyaxis.
Example —
figure
yyaxis left
plot(tsol, ans1(:,[1 2]))
yyaxis right
plot(tsol, ans1(:,[3 4]))
grid
legend('x','x_1','y','y_1')
You can experiment with different line styles as well.
Use the 'Location' name-value pair to put the legend where you want it.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by