フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Code not setting value=0

1 回表示 (過去 30 日間)
James
James 2013 年 6 月 21 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I don't understand this odd little thing in my code. I have simplified the function but this is basically what it's doing:
A=10;
fun=@(x) x+1/(A.*x);
z=fzero(fun,0)
This runs fine and gives me the result.
However if I do this...
A=10;
B=0;
fun=@(x) x+1/((A.*x)+(B.*x));
z=fzero(fun,0)
... then I get problems. I thought that this should give the same result! I don't get why defining B=0 doesn't seem to be taken into account when fzero solves fun.
The idea is that I can change B once I know that for B=0 I get the same result as for if there was no B term in the equation.
--- update ---
I have identified that the problem is with the x in the B.*x term. If I just do (A.*x)+B then it works fine. Because the fzero variable is x it seems to not recognise that whilst there is an additional x dependance it should not take it into account since B=0 makes that term 0.
  2 件のコメント
Matt J
Matt J 2013 年 6 月 24 日
This runs fine and gives me the result.
No, even your first example, doesn't work. The function is undefined at x=0 and fails as it should
z=fzero(fun,0)
Error using fzero (line 309)
Function value at starting guess must be finite and real.
Matt J
Matt J 2013 年 6 月 24 日
編集済み: Matt J 2013 年 6 月 24 日
The function
fun=@(x) x+1/((A.*x)+(B.*x));
can never have any roots when A+B>0. In the region x>0, both terms being summed in the function are strictly positive. The function is undefined at x=0. Inn the region x<0, all terms are strictly negative.

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 21 日
A=10;
B=1;
fun=@(x) x+1/((A.*x)+(B.*x));
z=fzero(fun,eps)
  1 件のコメント
James
James 2013 年 6 月 21 日
That doesn't seem to work. Still getting the problem that the B=0 result is not the same as when no B in the equation.

Matt J
Matt J 2013 年 6 月 24 日
編集済み: Matt J 2013 年 6 月 24 日
I think you might be missing some parentheses
fun=@(x) (x+1)/((A.*x)+(B.*x));
Otherwise, the function can't have any roots (see my Comments above).

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by