plot logistic model in matlab

To plot a logistic model in MATLAB, you can follow these steps:

  1. Define the logistic function, which is also known as the sigmoid function:
main.m
function y = logistic(x, params)
  y = params(1) ./ (1 + exp(-params(2)*(x-params(3))));
end
93 chars
4 lines

The logistic function takes three parameters:

  • params(1) is the maximum value of the curve (i.e., the upper asymptote)
  • params(2) is the steepness of the curve (i.e., the slope at the midpoint)
  • params(3) is the midpoint of the curve (i.e., the x-value at the inflection point)
  1. Load your data into MATLAB and define the x and y values:
main.m
x = [0, 1, 2, 3, 4, 5];
y = [0.1, 0.3, 0.4, 0.7, 0.9, 0.95];
61 chars
3 lines
  1. Fit the logistic model to the data using the lsqcurvefit function in MATLAB:
main.m
params0 = [1 1 1]; % Initial guess for the parameters
params_fit = lsqcurvefit(@logistic, params0, x, y);
106 chars
3 lines
  1. Plot the data and the fitted logistic curve:
main.m
figure;
plot(x, y, 'o');
hold on;
x_range = linspace(0, 5, 100);
y_fit = logistic(x_range, params_fit);
plot(x_range, y_fit);
xlabel('x');
ylabel('y');
legend('Data', 'Fitted Curve');
184 chars
10 lines

This will produce a graph with the data plotted as points and the fitted logistic curve as a line. The x and y axes are labeled, and there is a legend indicating which curve is which.

gistlibby LogSnag