matlab 编程

来源:百度知道 编辑:UC知道 时间:2024/05/13 04:27:54
求x*sinx在[-10,10]上的所有极值,用matlab编程
没这么复杂吧?
把关键的地方写出来就行

matlab源程序:

function [yy,yf,ymax]=test(a,b)
if ~(nargin==0 | nargin==2)
error('输入参数个数错误');
end
if nargin==0
a=-10;b=10;
end
clc;
if nargout>3
error('输出参数不能超过3个');
end
step=(b-a)/200;
stat=a;y=[];
eps=1.0e-6;
te=ff(stat);
while te < a & stat <= b
stat=stat+step;
te=ff(stat);
end
if te > b | stat > b
disp('该函数在 [-10,10] 内无极值点');
return
end
i=1;
stat=stat+step;
res(i)=te;
te=ff(stat);
while stat<=b
if ~insets(te,res)%判断找出的点是不是已经存在
i=i+1;res(i)=te;stat=stat+step;
else
stat=stat+step;
end
te=ff(stat);
end
n=0;
for i=1:length(res)
if res(i)>=a & res(i) <=b
n=n+1;y(n)=res(i);
end
end
res=[];n0=0;
for i=1:n
if checkss(y(i))
n0=n0+1