what's wrong here? I have copied this code from my textbook and it's not working

1 回表示 (過去 30 日間)
B
B 2015 年 5 月 5 日
コメント済み: B 2015 年 5 月 5 日
Help me here plz:
------------------------------------------------
function xb=insearch(func,xmin,xmax,ns)
if nargin <4;
ns=50;
end
x=linspace(xmin,xmax,ns);
f=func(x);
nb=0;
xb=[];
for k=1:length(x)-1;
if sign (f(k))~=sign(f(k+1))
nb=nb+1;
xb(nb,1)=x(k);
xb(nb,2)=x(k+1);
end
end
if isempty(xb)
disp('no brackets found')
disp('check interval or increase ns')
else
disp('number of brackets:')
disp(nb)
end
and In the command window:
insearch (@x sin(10*x)+cos(3*x),xmin,xmax,ns)
and am getting the following error:
Error: Unexpected MATLAB expression.

採用された回答

Walter Roberson
Walter Roberson 2015 年 5 月 5 日
insearch (@(x) sin(10*x)+cos(3*x),xmin,xmax,ns)
notice I changed the @x to @(x)

その他の回答 (1 件)

Ahmet Cecen
Ahmet Cecen 2015 年 5 月 5 日
Your problem is here:
insearch (@x sin(10*x)+cos(3*x),min,max,ans)
It is NOT:
@x sin(10*x)+cos(3*x)
It is:
@(x) sin(10*x)+cos(3*x)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by