Kindly find error in the code
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2020년 11월 1일
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
Star Strider
2020년 11월 2일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!