Problem fitting with lsqcurvefit
3 次查看(过去 30 天)
显示 更早的评论
im getting the following error while executing the code "Index exceeds the number of array elements (2)", the goal is to fit a curve to points using a certain Formula:
[name,path]=uigetfile({'*.xlsx'});
filename = [path name];
opts = detectImportOptions(filename);
preview(filename,opts);
newtable = readtable(filename);
x = newtable{:,1};
y = newtable{:,2};
fun = @(w,x) sqrt(w(1)^2+w(2)^2*(x-w(3))^2)
x0 = [1 1];
w = lsqcurvefit(fun,x0,x,y)
0 个评论
采纳的回答
Torsten
2022-9-12
M = [26 93
27 67.9
28 54.5
29 20.5
29.1 20.6
29.2 17.6
29.3 15.7
29.4 13
29.5 12
29.6 9.2
29.7 8.7
29.8 6
29.9 5.4
30 6.3
30.1 7.2
30.2 8.3
30.3 12.3
30.4 12.4
30.5 16.4
30.6 18.3
30.7 21.5
30.8 23.7
30.9 27.6
31 28.7
31.1 32.1
31.2 34.6
31.3 36.3
31.4 39.1
31.5 40.6
31.6 44.2
31.7 48.2
31.8 50.5
31.9 53.8
32 55.9
33 77.1];
x = M(:,1);
y = M(:,2);
fun = @(w) sqrt(w(1)^2+w(2)^2*(x-w(3)).^2);
fun2 = @(w)fun(w)-y;
w0 = [1 1 1];
w = lsqnonlin(fun2,w0)
hold on
plot(x,y,'o')
plot(x,fun(w))
hold off
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!