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

1 回表示 (過去 30 日間)
Gideon Gaiters
Gideon Gaiters 2022 年 4 月 25 日
回答済み: Aditya 2023 年 11 月 23 日
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 Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by