Clear Filters
Clear Filters

Help using solve function with inputting a known variable

1 view (last 30 days)
Hello all,
I'm using the solve function to solve a system of non-linear equations and I'm having a little trouble. I'm trying to input a function f into one of the equations, and then solve for the 3 variables. The problem is that it treats f as a symbolic variable. The order is also messed up, but I think I just need to specifically call the solutions for each variable.
Here is the code:
function kinematicanal
clc;
syms x1 y1 phi1;
for k=1:1:11
t=(k-1)/10;
f=sin(10*t);
[x1sol(k),y1sol(k),phi1sol(k)]=solve('x1-sin(phi1)','y1+cos(phi1)','phi1-f',x1,y1,phi1);
q=[x1sol;y1sol;phi1sol];
end
q
Can anyone help? Thanks, Ian
Edit: That should be a bit easier to read the code

Accepted Answer

Walter Roberson
Walter Roberson on 7 Feb 2011
[x1sol(k), y1sol(k), phi1sol(k)] = solve(x1-sin(phi1), y1+cos(phi1), phi1-f, x1, y1, phi1);
Question: why do you bother constructing q within the loop? You overwrite it each time through the loop, and you overwrite it with the complete vectors, so the end result would be the same as constructing it once after the loop.
Question 2: why are you not pre-allocating space for x1sol, y1sol, phi1sol?
Question 3: why do you not just do the solve() once outside of any loop, with k symbolic? You would get out a single x1sol, y1sol, phi1sol that was parameterized in terms of k. matlabFunction() the result if you need to, and feed it 1:11 and you will get all the answers at once.
Question 4: if t = (k-1)/10 and f=sin(10*t) and you do not use t for anything else, then why not skip t and go directly for f = sin(k-1) ?
Question 5: are you sure you want to be taking the sin() of an integer number of radians???
  12 Comments
Walter Roberson
Walter Roberson on 7 Feb 2011
double() the result to get IEEE double precision floating point.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!