Symbolic Solver dot indexing error and general understanding

7 views (last 30 days)
Hi,
I am working on an assignment for class and we must use the symbolic solver and vector math to determine the position of a multi bar system. Unfortunatly no matter what I try to do and other resources I look at, I cannot get the symbolic solver to output either a value for y alone that I can plug back into for theta5 or in the configuration I have shown, Syms both y and theta5 and try to find them together.
I keep getting the "dot indexing not supported" error when using y = eval(sol.y) but i think this is the correct syntax to call when having multiple solved variables.
If I were to do the first mentioned configuration where i only Syms y and later solve theta5 with that y value, I would use y=eval(sol) since the symbolic sovler is only haveing one value stored but this result gives y=[ ] and i have no idea why this result keeps happening either.
I'm sure there are other more efficient ways of solveing this but I really need to know what I am doing wrong with the Symbolic solver and how to use it
Thanks!
clear all;
clc;
%given
O4O2 = 6;%[6 15 24];
O2A = 4;
O4B = 30;
BC = 25;
O2xO6x = 5;
theta2= 45 %[0:360];
F = 200;
i=1;
while i<=length(theta2)
syms y theta5
O2 = [0 0 0];
O2A_ = O2A*[cosd(theta2) sind(theta2) 0];
O4O2_ = [O4O2 0 0];
O4A_ = O4O2_+O2A_;
theta1a = atand(O4A_(2)/O4A_(1));
O4B_ = O4B*[cosd(theta1a) sind(theta1a) 0];
O2B_ = O4O2_ + O4B_;
theta5 = atand((O2B_(2)-y)/(O2B_(1)-7));
BC_ = BC*[cosd(theta5) sind(theta5) 0];
O2C_ = [7 y 0];
eqn1 = -O2C_+ O2B_ + BC_;
sol = solve(eqn1(1),eqn1(2));
y = eval(sol.y)
i=i+1;
end
Dot indexing is not supported for variables of this type.
Error in sym/subsref (line 898)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in project_code (line 39)
y = eval(sol.y)
Edit: A few have mentioned the main issue is that my solve does not return any values, the system is unsolvable. Im not sure why this would be. If you look at the value for eq1 it returns an equation in terms of y. If I rewrite my code like below to make it output only for y, then use symbolic solver to say sol = eq1 solved for y, why cant it do this? I'm sure I could do this particular instance by hand If I wanted to.
Thanks again for all your help!
clear all;
clc;
%given
O4O2 = 6;%[6 15 24];
O2A = 4;
O4B = 30;
BC = 25;
O2xO6x = 5;
om_o2 = [0 0 180]
om_o2 = om_o2*((2*pi)/(60));
theta2= 45%[0:360];
F = 200;
i=1;
while i<=length(theta2)
O2 = [0 0 0];
O2A_ = O2A*[cosd(theta2) sind(theta2) 0];
O4O2_ = [O4O2 0 0];
O4A_ = O4O2_+O2A_;
theta1a = atand(O4A_(2)/O4A_(1));
O4B_ = O4B*[cosd(theta1a) sind(theta1a) 0];
O2B_ = O4O2_ + O4B_;
syms y
theta5 = atand((O2B_(2)-y)/(O2B_(1)-7));
BC_ = BC*[cosd(theta5) sind(theta5) 0];
O2C_ = [7 -y 0];
eqn1 = O2B_ + BC_;
sol = solve(eqn1,y)
% y = subs(y)
% theta5 = eval(sol.theta5)
%
% Va = cross(om_o2, O2A_);
i=i+1;
end
  3 Comments
Tyler Babinec
Tyler Babinec on 9 Nov 2021
My university teaches that this is the best way, so while i am sure that you are correct and wish that I could just learn things in the best way from the start, I am possitive we are supposed to use eval for symbolic expressions for this course and will unfortnunalty have to follow suit.
So since there are no solutions to my sol, does that mean that somewhere in the assignment I am doing some math wrong to get the no solutions? or is it instead that I am not doing something sytnax correctly?
Walter Roberson
Walter Roberson on 9 Nov 2021
expressionExpression to evaluate
character vector | string scalar
Expression to evaluate, specified as a character vector or string scalar. expression must be a valid MATLAB expression and must not include any MATLAB keywords. To determine whether a word is a MATLAB keyword, use the iskeyword function.
Notice that denies the possibility of eval() on a symbolic expression.
Observe:
syms x
expr = piecewise(1 <= x & x <=2, true, false)
expr = 
char(expr)
ans = 'piecewise(x in Dom::Interval([1], [2]), 1, symtrue, 0)'
eval(expr)
Error using sym/eval (line 13)
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
I am possitive we are supposed to use eval for symbolic expressions for this course and will unfortnunalty have to follow suit.
Tell your instructor to contact me, and I happy to prove to them that using eval() on a symbolic expression is a bug, and that it is a mistake to teach it to students.

Sign in to comment.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 9 Nov 2021
Because your equations don't have solutions, i.e.:
...
solve(eqn1(1,1), eqn(1,2)) % Check

Walter Roberson
Walter Roberson on 9 Nov 2021
syms y theta5
Okay, you create two symbolic variables there
O2 = [0 0 0];
O2A_ = O2A*[cosd(theta2) sind(theta2) 0];
O4O2_ = [O4O2 0 0];
O4A_ = O4O2_+O2A_;
theta1a = atand(O4A_(2)/O4A_(1));
O4B_ = O4B*[cosd(theta1a) sind(theta1a) 0];
O2B_ = O4O2_ + O4B_;
None of those lines use either of the symbolic variables, so all those variables are pure numeric.
theta5 = atand((O2B_(2)-y)/(O2B_(1)-7));
You overwrite one of the symbolic variables, theta5 before having used it. theta5 becomes a symbolic scalar variable involving the scalar symbolic variable y .
BC_ = BC*[cosd(theta5) sind(theta5) 0];
O2C_ = [7 y 0];
eqn1 = -O2C_+ O2B_ + BC_;
theta5 is scalar and involves the scalar symbolic variable y, and it is used in two places in BC_ so BC_ becomes a 1 x 3 symbolic expression involving the scalar symbolic variable y. Then on the next line, O2C_ becomes a 1 x 3 symbolic expression involving the scalar symbolic variable y. The third line adds the two and a numeric 1 x 3 variable, getting out as a result a 1 x 3 symbolic expression involving the symbolic variable y. Two of the three entries in eqn involve the scalar symbolic variable y.
sol = solve(eqn1(1),eqn1(2));
You extract the first two entries from eqn1, getting out two equations involving the scalar symbolic variable y, and not involving any other symbolic variable. So you are asking to solve a system of two symbolic expressions with respect to a scalar symbolic variable. This is an overdetermined system, and can work only if one of the two equations is a linear multiple of the other, which does not happen to be the case. You are asking to find a single scalar value for y that solves both equations at the same time. MATLAB says it does not know of any solution.
  1 Comment
Walter Roberson
Walter Roberson on 9 Nov 2021
Edited: Walter Roberson on 9 Nov 2021
%given
O4O2 = 6;%[6 15 24];
O2A = 4;
O4B = 30;
BC = 25;
O2xO6x = 5;
theta2= 45 %[0:360];
theta2 = 45
F = 200;
i=1;
while i<=length(theta2)
syms y theta5
O2 = [0 0 0];
O2A_ = O2A*[cosd(theta2) sind(theta2) 0];
O4O2_ = [O4O2 0 0];
O4A_ = O4O2_+O2A_;
theta1a = atand(O4A_(2)/O4A_(1));
O4B_ = O4B*[cosd(theta1a) sind(theta1a) 0];
O2B_ = O4O2_ + O4B_;
theta5 = atand((O2B_(2)-y)/(O2B_(1)-7));
BC_ = BC*[cosd(theta5) sind(theta5) 0];
O2C_ = [7 y 0];
eqn1 = -O2C_+ O2B_ + BC_;
string(eqn1)
sol = solve(eqn1(1),eqn1(2), 'returnconditions', true)
y_solutions{i} = subs(sol.y)
i=i+1;
end
ans = 1×3 string array
"25/(((140737488355328*y)/3880075451231215 - 83108190724539/250327448466530)^2 + 1)^(1/2) + 3880075451231215/140737488355328" "2576353912460709/281474976710656 - (25*((140737488355328*y)/3880075451231215 - 83108190724539/250327448466530))/(((140737488355328*y)/3880075451231215 - 83108190724539/250327448466530)^2 + 1)^(1/2) - y" "0"
sol = struct with fields:
y: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
y_solutions = 1×1 cell array
{0×1 sym}
Look at that first equation, listed under ans . It has sqrt(f(y)^2+1) + positive constant and that has to equal 0. But because of the addition of the positive constant, the sqrt() would have to be negative, which is never the case for sqrt(), by convention. sqrt(A) == B is not exactly the same as A == B^2. Suppose for example that B = -3, then the first of them is sqrt(A) = -3 and the second of them is A = (-3)^2 which is A = 9. But sqrt(9) ~= -3 . sqrt() does not take all possible roots.
You might potentially get different results if you use atan2d(y,x) instead of atand(y/x)

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!