fsolve with symbolics
2 views (last 30 days)
Show older comments
Hey all, I've been working on some code, and I have two questions:
1) If I'm using fsolve, and one of my guesses is zero, will matlab still use it as a guess?
2) My equations are really complicated, so I've created simplifications for them. The equations I put into fsolve are made up of variables that are defined in terms of other variables, and those other variables contain the values that fsolve is trying to solve. Unfortunately, when I run the code, Matlab doesn't recognize the undefined variables in the other terms, and "errors". Assigning these values as symbolics creates an error in fsolve. What should I do? Example below, where x, y, and z are the valued I want to solve for:
y_1 = 309;
n0_1 = 1;
%n1_1 = 1.580086;
n1_1 = x;
%k1_1 = 0;
k1_1 = y;
n2_1 = 5.07;
k2_1 = 3.62;
%d1_1 = 25;
d1_1 = z;
R_1 = .4335;
g1_1 = (n0_1.^2 - n1_1.^2 - k1_1.^2)./((n1_1 + n2_1).^2 + k1_1.^2);
g2_1 = (n1_1.^2 - n2_1.^2 + k1_1.^2 - k2_1.^2)./((n1_1 + n2_1).^2 + (k1_1 + k2_1).^2);
h1_1 = (2.*n0_1.*k1_1)./((n0_1 + n1_1).^2 + k1_1.^2);
h2_1 = (2.*(n1_1.*k2_1 - n2_1.*k1_1))./((n1_1 + n2_1).^2 + (k1_1 + k2_1).^2);
a_1 = (2.*pi().*k1_1.*d1_1)./y_1;
A_1 = 2.*(g1_1.*g2_1 + h1_1.*h2_1);
B_1 = 2.*(g1_1.*h2_1 - g2_1.*h1_1);
C_1 = 2.*(g1_1.*g2_1 - h1_1.*h2_1);
D_1 = 2.*(g1_1.*h2_1 + g2_1.*h1_1);
b_1 = (2.*pi().*n1_1.*d1_1)./y_1;
g1_1.^2 + h1_1.^2).*(exp(2.*a_1)) + (g2_1.^2 + h2_1.^2).*(exp(-2.*a_1)) + A_1.*(cos(2.*b_1)) + B_1.*(sin(2.*b_1)))./((exp(2.*a_1)) + (g1_1.^2 + h1_1.^2).*(g2_1.^2 + h2_1.^2).*(exp(-2.*a_1)) + C_1.*(cos(2.*b_1)) + D_1.*(sin(2.*b_1))) - R_1
This is just for one of the equations (you can see why I want to simplify. Suggestions?
--Thanks
1 Comment
Walter Roberson
on 24 Feb 2012
Cross reference: http://www.mathworks.com/matlabcentral/answers/30184-defining-variables-for-fsolve
Accepted Answer
Walter Roberson
on 24 Feb 2012
1) Yes, 0 is fine as a guess. As discussed in your other thread, the spacing of what you wrote mislead me.
2) You need to use double(subs()) like I showed in your previous question if you are going to use symbolic variables.
3) Rewrite the code before your fsolve() call to be an actual named routine that takes a single variable X. That routine would not use syms. Instead, the first thing you would do inside the routine is
x = X(1); y = X(2); z = X(3);
then continue on down to the construction of the matrix that you currently do in F, except make F a real matrix instead of a function handle. Return that matrix F from the new named routine. This new routine will operate purely numerically and so will not need subs() or double().
In a new driver routine, code the guess and the call to fsolve, passing just the handle of the new routine
fsolve(@YourRoutine, Guess)
0 Comments
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox 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!