How to generate a plot from an array and show the function/equation that verifies it?

1 view (last 30 days)
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
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
Panos
Panos on 5 Feb 2023
Sorry but the last example with interpolation, I cannot understand it.
Where are the coefficients?
Torsten
Torsten on 5 Feb 2023
Edited: Torsten on 5 Feb 2023
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.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!