Generating an error bar plot with a custom equation fit

6 views (last 30 days)
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)

VBBV
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
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)
y1 = 5×1
0.0315 0.1757 0.4698 0.9189 1.5022
% choose a polynomial order for your curve fit
y1_fit = fit(x,y1,'poly3')
y1_fit =
Linear model Poly3: y1_fit(x) = p1*x^3 + p2*x^2 + p3*x + p4 Coefficients (with 95% confidence bounds): p1 = -0.001309 (-0.01181, 0.009191) p2 = 0.08558 (-0.009518, 0.1807) p3 = -0.1052 (-0.3614, 0.151) p4 = 0.05286 (-0.1431, 0.2489)
h = plot(y1_fit,x,y1,'-o');
h(1).LineWidth = 2;
h(2).LineWidth = 1.5;
legend('Poly','Fitted Poly Curve')
Jason
Jason on 18 May 2023
Moved: VBBV on 18 May 2023
Hi, many thanks for all this, very much appreciated.
What i actually want to do though is NOT to fit to a polynomial. I want to take any imported y data, plot it and also plot the fit to a specified equation eg to make the y_fit here=ymax*(K*x).^n./(1+(K*x).^n)
Thats what i cant work out how to do

Sign in to comment.


Jason
Jason on 16 May 2023
Hugely appreciated thanks. Its the bit in the middle thats key for me...i'll do some digging on polyval
Thank you

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!