MATLAB Answers


Curve fitting in a for loop?

Asked by Kelly McGuire on 21 Feb 2019
I haven't tried this before, but I would like to run the code below in a for loop and have all of the data points and fitted curves plotted on the same plot. My data is in matrix form. I have 5 columns with concentrations (Concentration excel file), which should be xdata. There is also a average mortality excel file with 5 columns that have the average mortality, which should be ydata. Each cell in the aveMortality excel file lines up with a cell in the Concentration excel file. So, I would like the for loop to use the first Concentration column with the first column of the aveMortality matrix, and then the second columns, and so on. Then plot the curves as lines, and the xdata and ydata as large dots. Here is my code, and the excel files are attached:
xdata = Concentration;
ydata = aveMortality;
fun = @(x,xdata)(1./(1+(x(1)./xdata).^x(2)));
x0 = [180,1.6];
%x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
opts = optimset('MaxFunEvals',1E+4, 'MaxIter',1E+4 );
x = fminsearch(@(x) norm(ydata - fun(x,xdata)), x0, opts)
hold on
plot(xdata,ydata,'r.','markerszie', 30);
xlim([0 200]);
ylim([0 1]);
ylabel('Average Mortality','FontWeight','bold');
box off
y = 0:600;
xlim([0 200])
ylim([0 1])


Sign in to comment.

0 Answers