Kindly find error in the code
1 view (last 30 days)
Show older comments
Mrudul Agrawal
on 1 Nov 2020
Commented: Star Strider
on 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 Comments
Accepted Answer
Star Strider
on 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 Comments
Star Strider
on 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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!