Help with fzero equation solver
3 ビュー (過去 30 日間)
古いコメントを表示
I have a equation with 3 variables A,B,C and want to know for each A,B combination the value of C that makes the equation equal to zero.
The fzero function seems the best way to do this.
The code is like this (slight simplification to actual fun):
A=[0:1:10];
B=[0:1:10];
fun=C+(0.5.*(A+B));
C=fzero(fun)
I have looked at the mathworks help on fzero but can't get it to work.
I get the error that 'C' is unknown but that's what I'm trying to find!
0 件のコメント
回答 (2 件)
Tom
2013 年 6 月 20 日
There are a couple of things here: fzero only works with scalars, so you can't use it on all A and B values.
Secondly, you need to create a function handle to the function, using the @ sign.
So in your case, this is:
fun=@(C) C+(0.5.*(A+B));
Now using A = 10, B = 10 as an example
C=fzero(fun,0)
C =
-10
Tom
2013 年 6 月 20 日
There is a way to do this without a loop:
You have one unknown (C) and one equation (fun), and you are looking for
C + f(A,B) = 0
Therefore,
C = -f(A,B)
So now you can use BSXFUN to run the function using all the combinations of A and B:
fun=@(x,y) -(0.5.*(x+y));
bsxfun(fun,A,B')
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!