How can I perform a recursive input using the solve function?

6 views (last 30 days)
I've created some code to simulate a crank-rocker mechanism and I'm wondering how to perform an operation where I take the crank rotating 360 degrees and spit out all of the theta values for each corresponding link of the mechanism.
For reference: r1 = ground, r2 = crank, r3 = attachment link, r4 = rocker
t1, t2, t3, t4 are all thetas for reach respective link
Here is the code:
r1=4.2; r2=1; r3=5; r4=2.032;
t1=0; t2=0:1:360;
syms t3 t4 positive
eqn1 = r2.*cosd(t2)+r3.*cosd(t3)==r1.*cosd(t1)+r4.*cosd(t4);
eqn2 = r2.*sind(t2)+r3.*sind(t3)==r1.*sind(t1)+r4.*sind(t4);
S = solve(eqn1, eqn2, 'IgnoreAnalyticConstraints', true)
When I run the code, it gives me 4 error messages:
Second argument must be a vector of symbolic variables.
checkVariables(vars)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in sym/solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
I'm just looking to find a way to scale t2 from 0 to 360 degrees such that t3 and t4 give positive values only. I know how to do this with a single number, but once t2 becomes an array, I don't know how to solve those issues.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Dec 2020
t1=0; t2=0:1:360;
So t2 is a vector of length 361
eqn1 = r2.*cosd(t2)+r3.*cosd(t3)==r1.*cosd(t1)+r4.*cosd(t4);
With t2 being a vector of length 361, the left hand side of the == is a vector. The right hand side is a scalar, but expansion will be done so that you will get a vector of equations, one for each element of t2.
Likewise, eqn2 will be a vector of equations.
solve(eqn1, eqn2, 'IgnoreAnalyticConstraints', true)
with two vectors of length 361, would be trying to solve 722 simultaneous equations, in a total of two unknowns.
You are not going to be able to find a part of t3 and t4 values that manages to solve all 722 equations simultaneously .
You will need something like
arrayfun(@(E1, E2) solve(E1, E2, [t3, t4], 'IgnoreAnalyticConstraints', true), eqn1, eqn2, 'uniform', 0)
  1 Comment
Evan Anderson
Evan Anderson on 19 Dec 2020
Thanks for this. I ended up being able to use a while loop pretty easily, I just had to rearrange some things. Any and all help was appreciated!

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!