invalid expression. when calling a function or indexing a variable use parentheses.

조회 수: 7 (최근 30일)
invalid expression. when calling a function or indexing a variable use parentheses.
T= (0.5) *(4) * (;, Y, (3))
function [t,Y] = dynamicsProjectMurphy()
%This is a simple example of solving ode's in matlab. You'll mostly need
%to focus on defining F within the odefun currently between lines 10 and
%16.
%1D Y = [x,v]
%2D Y = [x,y,vx,vy]
%3D Y = [x,y,z,vx,vy,vz]
function dYdt = odefun(~,Y)
m1 = 4;
m2= 2;
k1 = 20;
k2= 20;
g= 9.81;
F1 = -k1*Y(1)+k2*(Y(2)-Y(1))-m1 * g;
F2 = k2 * (Y(1)-Y(2))-m2 *g;
a1 = F1/m1;
a2 = F2/m2;
dYdt = [Y(3);Y(4); a1; a2]; %dYdt = [v;a]
end
tspan = [0 10];%solve ode from t=0 to t=10
y0 = [1,1,0,0]; %start at x = 0.5 and v = 0
[t,Y] = ode45(@odefun,tspan,y0); %solve ode
figure(1)
plot(t,Y)
legend('x','v')
xlabel('t')
end
  댓글 수: 1
DGM
DGM 2022년 4월 26일
T= (0.5) *(4) * (;, Y, (3))
Yes, that's an invalid expression. I can't begin to guess what it's intended to mean. Not only is it invalid, I don't see where it's being used anywhere in the code and I don't see where it's getting Y either.

댓글을 달려면 로그인하십시오.

답변 (1개)

Aditya
Aditya 2023년 11월 23일
Hello Gideon,
It appears that you've encountered an "invalid expression" error in your code. The issue is due to the following line:
T = (0.5) * (4) * (;, Y, (3))
This line contains syntax errors. In MATLAB, when calling a function or indexing an array, you need to use parentheses appropriately. To correct this issue, you should update your code like this:
% Ensure Y is initialized with your data before this line
T = 2 * Y(:, 3);
Please make sure that the matrix `Y` is defined and contains at least three columns before this line is executed. If this line in question is not related to the rest of your function and is not needed, you may simply remove it.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by