Question on function handle
1 view (last 30 days)
Show older comments
Hi, everyone, Here are two questions on function handle.
I'm trying to make nonlinear simultaneous equations with 25 unknowns for fsolve run.
So,
F1(x1, x2, x3, ... x25) - a1 = 0; F2(x1, x2, x3, ... x25) - a2 = 0 ...
of all functions above, some functions have simple form like...
(1) x5+x6+x7+x8+...+x12=0 -> F2=@(x5,x6,...,x12)(x5+...+x12)
The problem is its tediousness. Is there an expression with sprintf or summation function instead of typing one by one?
and...
(2)
C=[c1 c2 ... c6] <- They are constant. cell 'Xpart' made of unknown x15 x16 ... x20 so, I wanna c1*x15+c2*x16+... c6*x20=0
--> F12=C.*Xpart (I know it is wrong expression, but it is what I want to show.)
Is there a function to express @(x15,..,x20)C.*Xpart?
Please help
S. R. Huh
2 Comments
Answers (1)
Matt J
on 12 Oct 2012
Edited: Matt J
on 12 Oct 2012
With fsolve, you don't even have the option of writing equations with 25 separate arguments in the tedious way you describe, e.g.,
F2=@(x5,x6,...,x12)(x5+...+x12)
As you will see in the documentation, fsolve requires you to bundle all of your unknowns into a single vector x and similarly F(x) needs to be expressed as a vector-valued function.
In general, when you have lots of variables and lots of equations, you use MATLAB vectorized operations to express them. So instead of the above equation where you manipulate x5...x12 individually, you might have
F2=@(x) sum(x(5:12))
When you have more than 1 equation, you look for similar vectorization methods to express all of them, or big pieces of them, at once. In your posted examples, in which all the equations are linear, you could hypothetically represent them as a matrix-vector multiplication
F=@(x) A*x
Although obviously MATLAB has better options than fsolve for solving purely linear systems.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!