필터 지우기
필터 지우기

fsolve with symbolics

조회 수: 1 (최근 30일)
Alex
Alex 2012년 2월 24일
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
Walter Roberson
Walter Roberson 2012년 2월 24일
Cross reference: http://www.mathworks.com/matlabcentral/answers/30184-defining-variables-for-fsolve

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 24일
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개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by