Generating an error bar plot with a custom equation fit
Show older comments
I am importing data into my workspace that defines variables x and y and an assocated standard deviation in y
I can plot with error bars representing the latter and i can also fit the data to a custom equation usign cftools
Is it possible to do both ie generate a plot with error bars representing the y data standard deviations and also the fit?
Is the only workaround to plot both the data and the equation?
The code i am using is:
x=Table{:,2} % curly arrows needed to pull variables from a table
y=Table{:,7} % the excel column that is the mean of the readings
% Assign a variable Dev to a table column (the standard deviation column)
dev=Table{:,8}
errorbar(x,y,dev,"-s","MarkerSize",10, "MarkerEdgeColor","red","MarkerFaceColor",[0.65 0.85 0.90], 'CapSize',18) % capsize specifies the size of the horizontal caps on the error bars
xlim([-300 6000])
xlabel('Cell Count')
ylabel('Response')
The equation that I want to fit is: y = ymax*(K*x)^n/(1+(K*x)^n)
any help hugely appreciated
Answers (2)
If you want to add multiple lines for errorbars i.e. both y data and y data fitted along with their deviations, you can plot them together as shown below
x = 0:4;
% y data from table with std deviation
y = [1 2 3 4 5];
% y data from the fitted equation
y1 = [2 3 4 5 6];
% Y data whole
y = [y;y1];
% standard deviation for y data from table & fitted equation
dev = [0.2 0.1 0.3 0.1 0.2;
0.1 0.3 0.4 0.3 0.1];
errorbar(x,y,dev); grid
xlim([-0.1 4.1])
legend('y-data','y-data (fitted)')
5 Comments
The SD of y-data from fitted equation (using polyfit and polyval functions) needs to evaluated and then appended to Y-vector . Later plot the data using errorbar
% x data from table
x = 1:5;
K = 0.1; % say
n = 2.5; % say
ymax = 10; % say
% y-data for fitted equation ( use polyfit & polyval functions)
y1 = ymax*(K*x).^n./(1+(K*x).^n)
dev1 = repmat(std(y1),1,5)
% Y data whole
% y = [y;y1];
% combined SD of table y-data & fitted equation y-data
% dev = [0.2 0.1 0.3 0.1 0.2; dev1]
% errorbar(x,y,dev);
Note that, here i have used constant (fixed) x-data points for fitted equation. You can use variable x-data points (with fixed range limits) for fitted equation to generate different SD values that match length of SD obtained from table y-data. one way to obtain it is by doing
x = randi([x_min x_max],1,length(y)); % length(y) from table
% x_min & x_max are range limits for x- data from table
3. this wont do what i wanted - i dont want to fit data to a simple polynomial but to a specified equation
For simple polynomial fit you can use fit function as shown and choose a suitable polynomial order for your data
% x data from table
x = [1:5].';
K = 0.1; % say
n = 2.5; % say
ymax = 10; % say
% y-data for fitted equation ( use polyfit & polyval functions)
y1 = ymax*(K*x).^n./(1+(K*x).^n)
% choose a polynomial order for your curve fit
y1_fit = fit(x,y1,'poly3')
h = plot(y1_fit,x,y1,'-o');
h(1).LineWidth = 2;
h(2).LineWidth = 1.5;
legend('Poly','Fitted Poly Curve')
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
