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

1 vue (au cours des 30 derniers jours)
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 commentaire
DGM
DGM le 26 Avr 2022
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.

Connectez-vous pour commenter.

Réponses (1)

Aditya
Aditya le 23 Nov 2023
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!

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by