Generating an error bar plot with a custom equation fit
6 views (last 30 days)
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
0 Comments
Answers (2)
VBBV
on 16 May 2023
Edited: VBBV
on 16 May 2023
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
VBBV
on 18 May 2023
Edited: VBBV
on 18 May 2023
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')
See Also
Categories
Find more on Interpolation 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!