多项式曲线拟和时遇到的 MATLAB问题

来源:百度知道 编辑:UC知道 时间:2024/06/07 23:40:39
测试的时候的问题:
1:当X和Y不等时,没有进入IF语句
2:时钟没有运行的感觉,就是最后的结果没有显示运行的时间!
测试的时候使用的数据!
X=[1 2 3]
y=[2 5 10]
A=mulitfit(X,Y,2)
此组数据测试时,结果符合!
使用
X=[1:2]
Y=[2 5 10]
A=mulitfit(X,Y,2)时,没有进入IF语句!
如下是我的代码
function A=mulitfit(X,Y,m)
tic
n=length(X);
m=length(Y);
if(n~=m)
fprintf('X is not match with Y\n');
disp('please input the another X and Y M\n');
X=input('X');
Y=input('Y');
M=input('m');
A=mulitfit(X,Y,M);
end
c(1:(2*m+1))=0;
b(1:(m+1))=0;
for j=1:(2*m+1)
for k=1:n
c(j)=c(j)+X(k)^(j-1);
if(j<(m+2))
b(j)=b(j)+Y(k)*X(k)^(j-1);
end
end
end
C(1,:)=c(1:(m+1));
for(i=2:m+1)
C(i,:)=c(i:(m+i));
end
A=b'\C;
toc

function A=mulitfit(X,Y,m)%m?次数?
tic
n=length(X);
m=length(Y);%????
if(n~=m) %?
fprintf('X is not match with Y\n');
disp('please input the another X and Y M\n');
X=input('X');
Y=input('Y');
M=input('m');
A=mulitfit(X,Y,M);
end

----------------
X=[1:2]%length=2
Y=[2 5 10]
A=mulitfit(X,Y,2)%m=2=length(x)