How to generate a plot from an array and show the function/equation that verifies it?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
Hallo.
I am an newbie in Matlab.
I have the following array:
s = [1640 2360 3116 3910 4743 5618 6537 7502 8515 9579 10696 11869 13100 14393 15751 17177 18673 20245 21895 23628 25447 27358 29364 31470 33681 36004 38442 41002 43690 46512 49476 52588 55696 59214 62953 66929 71156 75650 80427 85507 90907 96648 102752 109241 116140 123475 131273 139564 148378 157748 167711 178303 189563 201535 214263 227795 242181 257476 273737 291024]
the index of the array should starts from 1.
I want to generate a scatter plot where on X-axis I will have the index and on Y-axis I will have the above values.
Also, I would like to ask if there is a way to calculate the function/equation that generates the above values through the plot?
I assume that the function/equation should have the form of exponential like:
and normally it should be rounded.
If this is the case what kind of commands should I put in command prompt to have the scatter plot and the parameters a, b, c and d?
Thanks in advance.
Accepted Answer
Sulaymon Eshkabilov
on 4 Feb 2023
Here is how one can get the scatter plot of the given data with its sequential number:
s = [1640 2360 3116 3910 4743 5618 6537 7502 8515 9579 10696 11869 13100 14393 15751 17177 18673 20245 21895 23628 25447 27358 29364 31470 33681 36004 38442 41002 43690 46512 49476 52588 55696 59214 62953 66929 71156 75650 80427 85507 90907 96648 102752 109241 116140 123475 131273 139564 148378 157748 167711 178303 189563 201535 214263 227795 242181 257476 273737 291024];
x = 1:length(s);
scatter(x, s, 'filled')

x = x.';
s = s.';
% Fit model equation can be found using the followings:
H = fittype(@(a,b,c,d,x) (a/b)*c.^x-d, 'independent', {'x'});
y = fit(x,s, H) % The found coeff values for a,b,c,
Warning: Start point not provided, choosing random start point.
y =
General model:
y(x) = (a/b)*c.^x-d
Coefficients (with 95% confidence bounds):
a = 3.1 (-1.225e+08, 1.225e+08)
b = 2.835 (-1.12e+08, 1.12e+08)
c = -1.195 (-2.238, -0.1532)
d = -1.327e+04 (-4.085e+04, 1.431e+04)
9 Comments
Thank you very much for your response.
The result I get when run your commands in mathlab locally, i get
Warning: Start point not provided, choosing random start point.
> In curvefit.attention/Warning/throw (line 30)
In fit>iFit (line 307)
In fit (line 116)
y =
General model:
y(x) = (a/b)*c.^x-d
Coefficients (with 95% confidence bounds):
a = 0.6874 (-5.604e+07, 5.604e+07)
b = 0.0005425 (-4.423e+04, 4.423e+04)
c = 0.8995 (-21.49, 23.29)
d = -2.502 (-4.767e+04, 4.766e+04)
Should I change some in the configuration?
Also, when I tried to verify/calculate the initial values by replacing the computed values (both the ones produced here and the ones produced locally) of the coefficients in the equation I get results that have no relation with the initial values.
What I am doing wrong here?
Also, if I assume that the initial values are someway rounded, how can this be applied?
The parameters in your model are not independent. If a and b are parameters you have fitted, a*const and b*const give the same fit since the variable "const" cancels out in the division a/b.
Thus a "correct" model for your data could be a*exp(b*x) + c, e.g.
s = [1640 2360 3116 3910 4743 5618 6537 7502 8515 9579 10696 11869 13100 14393 15751 17177 18673 20245 21895 23628 25447 27358 29364 31470 33681 36004 38442 41002 43690 46512 49476 52588 55696 59214 62953 66929 71156 75650 80427 85507 90907 96648 102752 109241 116140 123475 131273 139564 148378 157748 167711 178303 189563 201535 214263 227795 242181 257476 273737 291024];
x = 1:length(s);
%scatter(x, s, 'filled');
x = x.';
s = s.';
% Fit model equation can be found using the followings:
H = fittype(@(a,b,c,x) a*exp(b*x)+c, 'independent', {'x'});
y = fit(x,s, H)%,'StartPoint',[1,1640]); % The found coeff values for a,c,d
Warning: Start point not provided, choosing random start point.
y =
General model:
y(x) = a*exp(b*x)+c
Coefficients (with 95% confidence bounds):
a = 8865 (8584, 9146)
b = 0.05843 (0.0579, 0.05895)
c = -5869 (-6612, -5126)
coefficientValues = coeffvalues(y);
figure(1)
plot(y,x,s)

figure(2)
plot(x,coefficientValues(1)*exp(coefficientValues(2)*x)+coefficientValues(3)-s)

Panos
on 4 Feb 2023
Thanks for your response.
if I try to verify your calculation for the first value (1640),
I get 8868*exp(0.05843)-5869 = 3.532594398497351e+03 = 3525.94
that has no relation with the initial value.
Is there any way for the calculation it to be more accurate?
If you are willing to loose precision for the end values - yes.
(See the error curve above).
Now the coefficients are balanced somehow such that the errors at the start and at the end are approximately equal in size.
Panos
on 4 Feb 2023
The purpose of all is, at the end, to have a function applied in order to replace the array completely.
So the accuracy of the verification process must be 100%.
Someone I knew years ago had managed to find these coeficients for the first 32 values and they were:
a: 96000, b: 7, c: 1.05, d: 12760.
From what I can if I leave only the first 32 values the above coefficients are confirmed.
So I will try to see what is going on with the rest.
Alternative fit model can be written by considering the value of 'a' for example:
clearvars
clc
close all
s = [1640 2360 3116 3910 4743 5618 6537 7502 8515 9579 10696 11869 13100 14393 15751 17177 18673 20245 21895 23628 25447 27358 29364 31470 33681 36004 38442 41002 43690 46512 49476 52588 55696 59214 62953 66929 71156 75650 80427 85507 90907 96648 102752 109241 116140 123475 131273 139564 148378 157748 167711 178303 189563 201535 214263 227795 242181 257476 273737 291024];
x = 1:length(s);
x = x.';
s = s.';
% Fit model equation can be found using the followings:
H = fittype(@(b,c,d,x) (1/b)*c.^x-d, 'independent', {'x'});
y = fit(x,s, H) % The found coeff values for a,b,c,
Warning: Start point not provided, choosing random start point.
y =
General model:
y(x) = (1/b)*c.^x-d
Coefficients (with 95% confidence bounds):
b = 0.0001157 (0.0001119, 0.0001195)
c = 1.061 (1.06, 1.061)
d = 5362 (4609, 6114)
plot(y, x, s)
grid on
legend('Data', 'Fitted Model')

bcd = coeffvalues(y);
Hvals= (1/bcd(1))*bcd(2).^x-bcd(3);
Diff = Hvals-s;
figure
plot(x, Diff, 'rd-')
grid on

Taking only 32 values for s, a perfect fit is possible as you see below. But for a longer array for s, the function can only be used as an approximation:
s = [1640 2360 3116 3910 4743 5618 6537 7502 8515 9579 10696 11869 13100 14393 15751 17177 18673 20245 21895 23628 25447 27358 29364 31470 33681 36004 38442 41002 43690 46512 49476 52588];
x = 1:length(s);
%scatter(x, s, 'filled');
x = x.';
s = s.';
% Fit model equation can be found using the followings:
H = fittype(@(a,b,c,x) a*exp(b*x)+c, 'independent', {'x'});
y = fit(x,s, H)%,'StartPoint',[1,1640]); % The found coeff values for a,c,d
Warning: Start point not provided, choosing random start point.
y =
General model:
y(x) = a*exp(b*x)+c
Coefficients (with 95% confidence bounds):
a = 1.371e+04 (1.371e+04, 1.372e+04)
b = 0.04879 (0.04879, 0.04879)
c = -1.276e+04 (-1.276e+04, -1.276e+04)
coefficientValues = coeffvalues(y);
figure(1)
plot(y,x,s)

figure(2)
plot(x,coefficientValues(1)*exp(coefficientValues(2)*x)+coefficientValues(3)-s)
grid on

In the list of known integer sequences, your vector s cannot be found:
If you want to reproduce all values exactly, you need interpolation and not approximation.
Use a spline, e.g.:
s = [1640 2360 3116 3910 4743 5618 6537 7502 8515 9579 10696 11869 13100 14393 15751 17177 18673 20245 21895 23628 25447 27358 29364 31470 33681 36004 38442 41002 43690 46512 49476 52588 55696 59214 62953 66929 71156 75650 80427 85507 90907 96648 102752 109241 116140 123475 131273 139564 148378 157748 167711 178303 189563 201535 214263 227795 242181 257476 273737 291024];
t = 1:numel(s);
fun = @(x) interp1(t,s,x,'spline');
tinter = t(1):0.01:t(end);
figure(3)
hold on
plot(t,s,'o')
plot(tinter,fun(tinter))
hold off
grid on

Panos
on 5 Feb 2023
Sorry but the last example with interpolation, I cannot understand it.
Where are the coefficients?
A spline is a polynomial of a certain degree in each subinterval [x(i) x(i+1)] with some compatibility conditions in the end points to the polynomials in the preceeding and following interval. So you have coefficients in each subinterval - how many will depend on the degree of the polynomial. You are still interested in the many, many coefficients that result from this approach ?
But since a spline does not allow to predict values for s that are outside your vector x, I think it won't work for your purpose.
More Answers (0)
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)