MATLAB Answers

How can I speed up vpasolve?

13 views (last 30 days)
I have the following equation that has no analytical inverse
where are constants. However, I would like to be able to find for a given z.
I am able to do this with the following code,
syms z thetaz
a = 1;
b = 2;
theta0 = 0;
eqn = (a*sin(theta0)-b*sin(thetaz))./(a*cos(theta0)+b*cos(thetaz)) == z;
v = 0.12; % given value of z
S = vpasolve(eqn,z == v);
vv = S.thetaz %associated value of thetaz
vv = 
but I have thousands of values of v so this takes a very long time.
I have tried using a matlabFunction to speed this up but I'm not sure how to input a z value to give a thetaz value back.
syms z thetaz
a = 1;
b = 2;
theta0 = 0;
z = (a*sin(theta0)-b*sin(thetaz))./(a*cos(theta0)+b*cos(thetaz));
f1 = matlabFunction(z);
check = f1(0.12)
check = -0.0802
The above code thinks I would like to find z from thetaz, is there a way to find thetaz from z using a matlabFunction?
Alternatively is there a way to speed up vpasolve?
Many thanks.

Accepted Answer

John D'Errico
John D'Errico on 12 Aug 2021
Edited: John D'Errico on 12 Aug 2021
Seriously, it has no solution? Gosh, am I surprised. So what does this do?
syms z thetaz
a = 1;
b = 2;
theta0 = 0;
eqn = (a*sin(theta0)-b*sin(thetaz))./(a*cos(theta0)+b*cos(thetaz)) == z;
thsol = solve(eqn,thetaz,'returnconditions',true)
thsol = struct with fields:
thetaz: [3×1 sym] parameters: [1×1 sym] conditions: [3×1 sym]
ans = 
ans = 
ans = 
So there are infinitely many solutions. Each a multiple of 2*pi away from each other. We can arbitrarily choose the primary solutions, thus with k==0.
syms k
thsol = subs(thsol.thetaz,k,0)
thsol = 
Still not very interesting, but now if we do this:
thsol = matlabFunction(thsol);
ans =
3.0818 - 0.0000i -0.1790 - 0.0000i 3.1416 + 0.6931i
now we find three trivially easy to obtain solutions, two of them are real, one is complex.
But I guess no analytical solution exists, because you said so. :)
  1 Comment
Finn Allison
Finn Allison on 12 Aug 2021
Yes I should have thought of that! Thank you for your explanation and speedy reply.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!