how to transfer expression to myfun using fsolve?

2 views (last 30 days)
Hi, I am trying to solve a system of nonlinear equations. I have the command in a for loop and each time the number of equations changes. So, I decided to create the expressions using 'sprintf' and than go for fsolve.
My function looks now like that:
function P = myfun(start_r)
eval(variables_R) % includes r1 = r(1); r2 = r(2); etc.
eval(equations) % includes P(1) = .....r1 + .....r2^2 - .....; P(2) = ...
So, the error message I receive is: "Undefined function or variable 'variables_R'" which is clear as I have to handle it to myfun.m. How to do that? Where to put variables_R in this expression fsolve(@myfun,start_r)?
Thank a lot for your help in advance. F

Answers (3)

Walter Roberson
Walter Roberson on 21 Jan 2013
If you have access to the Symbolic Toolbox, consider describing your system in terms of symbolic variables, and then subs() in any constants you have, and then solve() the system. Or if it is too messy for analytic solutions, using the MuPAD numeric::solve routine.

Franziska
Franziska on 21 Jan 2013
Thanks for the fast response!
I tried solving it with symbolic variables. It is fine for 3 equations, takes more than 2 hours 4 hours for 5.... and my goal is to realize it for about 70 - 100. So, this is not an option.
I have never worked with MuPad. I just checked it out now, but don't see how it might help me. I want to keep the problem in the normal Matlab-routine since the fsolve command it only one line out of more than 1500 in the code.

Franziska
Franziska on 22 Jan 2013
Ok, here is a way to do it. Define the expressions as global variables. This is not a good way, but it works for now. I am still looking for a more intelligent way...

Tags

Community Treasure Hunt

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

Start Hunting!