Help using solve function with inputting a known variable

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

[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

Sorry, those questions do not help. I'm answering a specific problem and I need to write the code in a specific format. I would like to know what t and f are different times without having to back calculate. And yes, I want the sine of an integer using radians.
Also my computer computes this in about 20 milliseconds, I'm not really worried about efficiency at this point.
Could someone help me find out how to plug in variable f into the solve function, I don't really need any other feedback right now.
I appreciate the help,
Ian
The line of code I gave *does* plug f into the solve function.
If it is strictly necessary (due to external constraints) to pass quoted strings instead of symbolic expressions, then:
[x1sol(k), y1sol(k), phi1sol(k)] = solve('x1-sin(phi1)', 'y1+cos(phi1)', subs('phi1-f'),x1, y1, phi1);
Oh...Sorry, I thought that was a quote from the original post. I had tried symbolic expressions first, I keep getting an error similar to this:
"
Warning: 2 equations in 4 variables. New variables might be introduced.
Warning: Could not extract individual solutions. Returning a MuPAD set object.
> In solve>assignOutputs at 104
In solve at 87
In kinematicanal at 12
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> kinematicanal at 12
[x1sol(k), y1sol(k), phi1sol(k)] = solve(x1-sin(phi1), y1+cos(phi1), phi1-f, x1, y1, phi1);"
I tried using subs and get the same thing. Thanks very much for your help, I thought you only criticized my code in the first post and didn't answer the question. I appreciate the feedback.
Could I ask you to go back to the original question and edit it so that each line of code is formatted on to a line of its own? It might just mean adding two spaces to the beginning of each code line.
I want to be sure that I am working with the same code you are working with; the forum has wrapped some of the lines automatically in ways that might be deceiving me.
Ah, the first iteration through, f is sin(0) which will be 0, so phi1 - f will be phi1, so that part is being interpreted as a symbolic variable name rather than something to be solved for 0.
[x1sol(k), y1sol(k), phi1sol(k)] = solve('x1-sin(phi1)=0', 'y1+cos(phi1)=0', subs('phi1-f=0'), x1, y1, phi1);
Hmmm...It doesn't seem to be substituting in the value for f still. No errors though :).
Thanks for the code cleanup. I can now be sure that the problem was what I outlined above about phi1 - 0 being treated as phi1
You could try subs('phi1-f=0','f',f)
but in theory that should not be needed.
For debugging, before the solve line you might want to display the result of
subs('phi1-f=0')
and
subs('phi1-f=0','f',f)
to see which subs() is working (if either.)
Awesome, yes the second works!! Thanks!! Only problem, it displays in 16bit fractions... I can get that working though.
double() the result to get IEEE double precision floating point.
Got it, thanks a lot for your help!

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!