Hi,
I understand that you want to fit a subset of the dataset using the selected non-linear function. The 'lsqcurvefit' function is not fitting the curve properly, possibly due to not including all the data points or, in some cases, including data points from other datasets.
To resolve this problem, you can try the following fixes,
- Copy all the datapoints before calling “lsqcurvefit"
x_data (:,j) = epsilonampl(selected_rowns, j);
y_data (:,j) = eliminierung_campl(selected_rowns, j);
- Fit the curve by passing the set of datapoints
x = lsqcurvefit(fun,x0,x_data(:,j),y_data(:,j));
times = linspace(min(x_data(:,j)),max(x_data(:,j)));
plot(x_data(:,j),y_data(:,j),'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')
Please find the complete code and its output below,
x_data = zeros(length(j_vals), length(i_vals));
y_data = zeros(length(j_vals), length(i_vals));
eave_data = zeros(length(j_vals), length(i_vals));
Ce_data = zeros(length(j_vals), length(i_vals));
x_data (:,j) = epsilonampl(selected_rowns, j);
y_data (:,j) = eliminierung_campl(selected_rowns, j);
fun = @(x,x_data)x(1)*exp(x(2)*x_data);
x = lsqcurvefit(fun,x0,x_data(:,j),y_data(:,j));
times = linspace(min(x_data(:,j)),max(x_data(:,j)));
plot(x_data(:,j),y_data(:,j),'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')
For more information, please refer the following resources -
Hope this is helpful.