Plotting with lsqnonlin regression

조회 수: 4 (최근 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.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by