Kindly find error in the code
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mrudul Agrawal
am 1 Nov. 2020
Kommentiert: Star Strider
am 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
0 Kommentare
Akzeptierte Antwort
Star Strider
am 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 Kommentare
Star Strider
am 2 Nov. 2020
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!