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

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 个评论

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 个)

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!

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2022a

回答:

2023-11-23

Community Treasure Hunt

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

Start Hunting!

Translated by