Can't understand why does it say "The expression to the left of the equals sign is not a valid target for an assignment."
2 ビュー (過去 30 日間)
古いコメントを表示
1- function dydt = lagrang(~, y)
2- syms m1 m2 m3 l1 l2 l3 g
3- dydt = zeros(6,5,4,3,2,1);
4- m1=1; m2=1; m3=1; l1=1; l2=1; l3=1; g=9.81;
5- dydt(1)=y(2);
6- dydt(3)=y(4);
7- dydt(5)=y(6);
8- dydt(1)*(l1^2*(m1+m2+m3))+dydt(3)((m2+m3)*l1*l2*cos(y(1)-y(3)))+dydt(5)*(m3*l1*l3*cos(y(1)-y(5)))=-(m2+m3)*l1*l2*y(4)^2*sin(y(1)-y(3))-m3*l1*l3*y(6)^2*sin(y(1)-y(5))-(m1+m2+m3)*g*l1*cos(y(1));
9- dydt(1)*((m2+m3)*l1*l2*cos((y(1)-y(3))))+dydt(3)*(l2^2*(m2+m3))+dydt(5)*(m3*l2*l3*cos(y(3)-y(5)))=(m2+m3)*l1*l2*y(2)^2*sin(y(1)-y(3))-m3*l2*l3*y(6)^2*sin(y(3)-y(5))-(m2+m3)*g*l2*cos(y(3));
10- dydt(1)*(m3*l1*l3*cos(y(1)-y(5)))+dydt(2)*m3*l2*l3*cos(y(3)-y(5))+dydt(l3^2*m3)=m3*l1*l3*y(2)^2*sin(y(1)-y(5))+m3*l2*l3*y(4)^2*sin(y(3)-y(5))-m3*g**l3*cos(y(5))
%other part
dydt = [0; pi/2; 0; pi/2; 0; pi/2];
[t1,y1] = ode45(@lagrang, [0 20], dydt);
plot(t1,y1(:,2));
When I run this, I get a problem. Sorry if this is a simple problem to solve, since I am new to this program, it's been one week for me since i started.
Error: File: lagrang.m Line: 8 Column: 98
The expression to the left of the equals sign is not a valid target for an assignment.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in lagrange_ans (line 2)
[t1,y1] = ode45(@lagrang, [0 20], dydt);
The equations' itself:
![Adsız4.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200024/Ads%C4%B1z4.png)
![Adsız5.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200025/Ads%C4%B1z5.png)
Where do i go wrong?
採用された回答
madhan ravi
2019 年 1 月 2 日
syms theta1(t) theta2(t) theta3(t)
m1=1; m2=1; m3=1; l1=1; l2=1;
l3=1; g=9.81; tau1=1; tau2=1; tau3=1;
e1= diff(theta1,2)*(l1^2*(m1+m2+m3)+l1)+...
diff(theta1,2)*((m2+m3)*(l1*l2*cos(theta1-theta2)))+...
diff(theta3,2)*(m3*l1*l3*cos(theta1-theta3))==...
-(m2+m3)*(l1*l2*diff(theta2)^2*sin(theta1-theta2))-...
m3*l1*l3*diff(theta3)^2*sin(theta1-theta3)-...
(m1+m2+m3)*g*l1*cos(theta1)+tau1-tau2;
e2= diff(theta1,2)*((m2+m3)*l1*l2*cos(theta1-theta2))+...
diff(theta2,2)*(l2^2*(m2+m3)+l2)+...
diff(theta3,2)*(m3*l2*l3*cos(theta2-theta3))==...
(m2+m3)*l1*l2*diff(theta1)^2*sin(theta1-theta2)-...
m3*l2*l3*diff(theta3)^2*sin(theta2-theta3)-...
(m2+m3)*g*l2*cos(theta2)+tau2-tau3;
e3= diff(theta1,2)*(m3*l1*l3*cos(theta1-theta3))+...
diff(theta2,2)*(m3*l2*l3*cos(theta2-theta3))+...
diff(theta3,2)*(l3^2*m3+l3)==...
m3*l1*l3*diff(theta1)^2*sin(theta1-theta3)+...
m3*l2*l3*diff(theta2)^2*sin(theta2-theta3)-...
m3*g*l3*cos(theta3)+tau3;
vars = [theta1(t); theta2(t); theta3(t)]
V = odeToVectorField([e1,e2,e3])
M = matlabFunction(V,'vars', {'t','Y'})
interval = [0 20]; %time interval
y0 = [0; pi/2; 0; pi/2; 0; pi/2]; %initial conditions
ySol = ode45(M,interval,y0);
tValues = linspace(interval(1),interval(2),1000);
for i = 1:6 % denotes the number of solutions
yValues = deval(ySol,tValues,i); % i denotes the solution number
plot(tValues,yValues)
hold on
end
Plot of 6 solutions:
![Screen Shot 2019-01-02 at 8.08.16 PM.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200046/Screen%20Shot%202019-01-02%20at%208.08.16%20PM.png)
5 件のコメント
その他の回答 (2 件)
Steven Lord
2019 年 1 月 2 日
編集済み: Steven Lord
2019 年 1 月 2 日
When you use the equals sign to perform assignment, the left-hand side must be a variable or a "piece" of a variable. The following work:
x = 42; % Assigning to a variable
y([2 3 5]) = [1 2 3] % Assigning to a "piece" of a variable
S.z(3) = -999 % Assigning to a piece of a field of a struct array
Let's look at your line 8. I've broken it into pieces using ... so we can read it without wrapping.
dydt(1)*(l1^2*(m1+m2+m3)) ...
+dydt(3)((m2+m3)*l1*l2*cos(y(1)-y(3))) ...
+dydt(5)*(m3*l1*l3*cos(y(1)-y(5))) = ...
-(m2+m3)*l1*l2*y(4)^2*sin(y(1)-y(3)) ...
-m3*l1*l3*y(6)^2*sin(y(1)-y(5))...
-(m1+m2+m3)*g*l1*cos(y(1));
You're trying to assign the result of the computations on the right side of the equals sign to another expression. MATLAB doesn't know what you're trying to assign here. Are you trying to update dydt(1), dydt(3), and dydt(5)? Are you trying to update y(1), y(3), and y(5)?
Because MATLAB can't figure out what you're trying to do, it throws an error. You will need to rewrite your higher order ODE as a system of first-order ODEs as shown in the "Solve Nonstiff Equation" example on the documentation page for ode45. Depending on whether some of those values are constants you may want to use a mass matrix to make rewriting your system a bit easier. See the fem2ode and batonode example files that show how to use a mass matrix.
3 件のコメント
Steven Lord
2019 年 1 月 2 日
Have you tried the MATLAB Onramp course on this page? That's designed to teach you the basics quickly and I believe it's only two or three hours long (with exercises and videos.)
KSSV
2019 年 1 月 2 日
編集済み: KSSV
2019 年 1 月 2 日
Don't use syms
function dydt = lagrang(~, y)
dydt = zeros(6,5,4,3,2,1);
m1=1; m2=1; m3=1; l1=1; l2=1; l3=1; g=9.81;
dydt(1)=y(2);
dydt(3)=y(4);
dydt(5)=y(6);
dydt(1)*(l1^2*(m1+m2+m3))+dydt(3)((m2+m3)*l1*l2*cos(y(1)-y(3)))+dydt(5)*(m3*l1*l3*cos(y(1)-y(5)))=-(m2+m3)*l1*l2*y(4)^2*sin(y(1)-y(3))-m3*l1*l3*y(6)^2*sin(y(1)-y(5))-(m1+m2+m3)*g*l1*cos(y(1));
dydt(1)*((m2+m3)*l1*l2*cos((y(1)-y(3))))+dydt(3)*(l2^2*(m2+m3))+dydt(5)*(m3*l2*l3*cos(y(3)-y(5)))=(m2+m3)*l1*l2*y(2)^2*sin(y(1)-y(3))-m3*l2*l3*y(6)^2*sin(y(3)-y(5))-(m2+m3)*g*l2*cos(y(3));
dydt(1)*(m3*l1*l3*cos(y(1)-y(5)))+dydt(2)*m3*l2*l3*cos(y(3)-y(5))+dydt(l3^2*m3)=m3*l1*l3*y(2)^2*sin(y(1)-y(5))+m3*l2*l3*y(4)^2*sin(y(3)-y(5))-m3*g**l3*cos(y(5))
%other part
dydt = [0; pi/2; 0; pi/2; 0; pi/2];
[t1,y1] = ode45(@lagrang, [0 20], dydt);
plot(t1,y1(:,2));
6 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!