Effacer les filtres
Effacer les filtres

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 vues (au cours des 30 derniers jours)
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 Adsız5.png
Where do i go wrong?

Réponse acceptée

madhan ravi
madhan ravi le 2 Jan 2019
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
  5 commentaires

Connectez-vous pour commenter.

Plus de réponses (2)

Steven Lord
Steven Lord le 2 Jan 2019
Modifié(e) : Steven Lord le 2 Jan 2019
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 commentaires
Steven Lord
Steven Lord le 2 Jan 2019
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.)
Arda Nova
Arda Nova le 3 Jan 2019
I didn't but I will. Although it's too late for project, I want to learn the program properly. Thank you.

Connectez-vous pour commenter.


KSSV
KSSV le 2 Jan 2019
Modifié(e) : KSSV le 2 Jan 2019
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 commentaires
Arda Nova
Arda Nova le 2 Jan 2019
used = for - ??
used ** for * ??
I couldn't spot that kind of mistakes? I honestly don't know what you say. Maybe missing something I should know.
"dydt = zeros(6,1);" I figured this out myself too, but couldn't put in the code in proper way to be honest.
Arda Nova
Arda Nova le 2 Jan 2019
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(2)*(l1^2*(m1+m2+m3))+dydt(4)((m2+m3)*l1*l2*cos(y(1)-y(3)))+dydt(6)*(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(2)*((m2+m3)*l1*l2*cos((y(1)-y(3))))+dydt(4)*(l2^2*(m2+m3))+dydt(6)*(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(2)*(m3*l1*l3*cos(y(1)-y(5)))+dydt(4)*m3*l2*l3*cos(y(3)-y(5))+dydt(6)(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))
I tried this instead, but nothing changed, which was what I expected.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by