Generating an error bar plot with a custom equation fit

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

Antworten (2)

VBBV
VBBV am 16 Mai 2023
Bearbeitet: VBBV am 16 Mai 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 Kommentare

Jason
Jason am 16 Mai 2023
Verschoben: VBBV am 16 Mai 2023
Hi..many thanks. I think you are basically saying the workaround is to generate two sets of y data...that plotted from raw with an SD error bar and that generated directly from an Eqn that i would like to compare the data to.
Its not obvious to me how i generate the SD from the fitted equation?
Many thanks
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)
y1 = 1×5
0.0315 0.1757 0.4698 0.9189 1.5022
dev1 = repmat(std(y1),1,5)
y1 = 1×5
0.5987 0.5987 0.5987 0.5987 0.5987
% 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
Jason
Jason am 17 Mai 2023
Verschoben: VBBV am 18 Mai 2023
Hi, having played arounf with polyfit and polyval my current interpretation is:
  1. polyfit generates coefficients for a polynomial fit at a specified order
  2. polyval generates an extrapolation through these coefficients ie "the fit"
  3. this wont do what i wanted - i dont want to fit data to a simple polynomial but to a specified equation
Am i right?
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 am 18 Mai 2023
Verschoben: VBBV am 18 Mai 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

Melden Sie sich an, um zu kommentieren.

Jason
Jason am 16 Mai 2023

0 Stimmen

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

Produkte

Version

R2023a

Gefragt:

am 16 Mai 2023

Verschoben:

am 18 Mai 2023

Community Treasure Hunt

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

Start Hunting!

Translated by