Multiple solutions with specific range input

8 ビュー (過去 30 日間)
Lee Lee
Lee Lee 2017 年 4 月 16 日
回答済み: Stephen23 2017 年 4 月 16 日
Here is my function: fun(x) = abs(abs(abs(x)-5)-2) and the range of input x: -5~7
If fun(x)= 1, I know the solutions of x are -4,-2,6
But how can I realize it with MATLAB ? Thanks.

回答 (1 件)

Stephen23
Stephen23 2017 年 4 月 16 日
Actually there are more solutions:
>> fun = @(x)abs(abs(abs(x)-5)-2);
>> foo = @(x)fun(x)-1;
>> x = -5:0.1:7;
>> idx = find(diff([1,diff(foo(x))>0,1])~=0);
>> baz = @(a,b)fzero(foo,x([a,b]));
>> out = arrayfun(baz,idx(1:end-1),idx(2:end))
out =
-4 -2 2 4 6
And you can see the solutions here:
plot(x,fun(x),'-',out,ones(size(out)),'o')

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by