invalid expression. when calling a function or indexing a variable use parentheses.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
DGM
am 26 Apr. 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.
Antworten (1)
Aditya
am 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!
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!