Plotting with lsqnonlin regression

1 回表示 (過去 30 日間)
Valeria Villegas-Medina
Valeria Villegas-Medina 2020 年 9 月 19 日
回答済み: Gaurav Garg 2020 年 9 月 21 日
Hello,
I wrote a function that uses lsqnonlin to generate predictions but I'm wondering how I can use those predictions to make plots and visulaize them? Should I be using lsqcurvefit instead? Any and all suggestions are welcome! The function lsq calls errFun and I've included them below for reference. Thank you in advance for your help!
function results = lsq(M) % takes Nx3 matrix where columns are Lx, Ly, gap
results = table();
tempTable = table();
idx = nchoosek(1:size(M,1), 3); % Nx3 matrix of all index trios
a = [];
Lx = [];
Ly = [];
gap = [];
for k = 1:size(idx, 1)
ix = idx(k ,:); % current idx trio, row vector
Lx = M(ix,1)';
Ly = M(ix,2)';
gap = M(ix,3);
L = sqrt((M(ix,1).^2 + M(ix,2).^2)/2);
% Here is where I'm having trouble. I passed the four arguments and the
% errors says "Too many input argumetns".
lsq = lsqnonlin(@(coeff) errFun(coeff, L, M(ix,3)), [0; 1; 1]);
a(k) = lsq(1);
tempTable.Lx = Lx(:)';
tempTable.Ly = Ly( :)';
tempTable.L = L(:)';
tempTable.Prediction = a(k)';
results = [results;tempTable];
end
results.PercentErr = abs((results.Prediction - 4.5670) ./ 4.5670) * 100;
end
function fErr = errFun(coeff, xdata, ydata)
%parameters
a = coeff(1);
b = coeff(2);
c = coeff(3);
% calculate prediction from model
yModel = a + b*exp(-c .* xdata);
fErr = yModel - ydata;
end

回答 (1 件)

Gaurav Garg
Gaurav Garg 2020 年 9 月 21 日
Hi Valeria,
You can look at the examples here to check how to plot and visualize the results.

Community Treasure Hunt

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

Start Hunting!

Translated by