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

조회 수: 12 (최근 30일)
tobarib
tobarib 2022년 11월 20일
답변: kaxaro 2022년 11월 20일
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) ,路径的话我是放在桌面的

채택된 답변

kaxaro
kaxaro 2022년 11월 20일
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求出来的是系数,你没有用,画图时肯定要把系数带入到函数中的。

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!