数据拟合问题求大神指教。

10 views (last 30 days)
tobarib
tobarib on 20 Nov 2022
Answered: kaxaro on 20 Nov 2022
clear ;
clc;
x=0.001:0.001:0.241;
z=load('C:\Users\Administrator\Desktop\y.txt');
a=mean(z);
y=z-a;
f=@(c,x)(c(1)*sin(c(2)*pi*x+c(3)));
c0=[0.016,250,-2];
lsqcurvefit(f,c0,x,y);
plot(x,y,'.-',x,f(c,x),'r:x')
legend('原始数据','拟合数据')
错误显示: 错误使用 lsqcurvefit (line 248)Function value and YDATA sizes are not equal.
出错 Untitled2 (line 9)
lsqcurvefit(f,c0,x,y);
请问是哪里出了问题,下面是文件y.txt的附件 y.txt (2.59 KB, 下载次数: 17) ,路径的话我是放在桌面的

Accepted Answer

kaxaro
kaxaro on 20 Nov 2022
x=0.001:0.001:0.241;
y=sin(x)+0.1;
f=@(c,x)(c(1).*sin(c(2).*pi.*x+c(3)));
c0=[0.016,250,-2];
cc=lsqcurvefit(f,c0,x,y)
plot(x,y,'.-',x,f(cc,x),'r:x')
legend('原始数据','拟合数据')
把上面xy 替换成你自己的数据
错误:1. 定义匿名函数用点乘表示元素间的乘法,按你的意思这里应该是.*而不是*
2.lsqcurvefit求出来的是系数,你没有用,画图时肯定要把系数带入到函数中的。

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!