fitting an equation to the curve

I have set of data X,Y and i would like to fit an equation of the form
.
I want to find for which n, the curev fits the best.
Any help!

9 Comments

What have you tried so far? And where are x and y in your equation?
I hope i am clear now.
I just plotted plot(X,Y), now i need to fit this
And n is an integer greater than 0 ?
Torsten
Torsten on 15 Nov 2018
Edited: Torsten on 15 Nov 2018
X=...;
Y=...;
nmax=10;
for i=1:nmax
Ysim=(sin(X)).^i;
error(i) = sum((Y-Ysim).^2);
end
[~,iopt]=min(error)
it is not working.
Torsten
Torsten on 15 Nov 2018
Edited: Torsten on 15 Nov 2018
Could you be a bit more precise ?
Did you fill the dots with your data ? What's the error message ?
You really shouldn't shadow the error function with a variable. Once you rename it, this code should work. I tried implementing fminsearch, but it is difficult to find a good way to select only discrete results.
It also works with shadowing :-) But I admit that it's not advisable.

Sign in to comment.

Answers (3)

Read about polyfit
N = 100 ; n = 2 ;
x = linspace(0,2*pi,N) ;
y = sin(x).^n ;
%
p = polyfit(x,y,7); % you can change 7 here...
x1 = linspace(0,2*pi);
y1 = polyval(p,x1);
figure
plot(x,y,'o')
hold on
plot(x1,y1)
hold off

5 Comments

@KSSV, thank you very much for your kind response.
It really worked for my case!
Glad to help you. Read about polyfit, know the subject behind it..don't blindly follow the functions..:)
I am sorry, It didnt work. there is some error i guess
It is not the polyfit that i want.
I want the equation of the form Y = after i should fit the curve.
In the above given example as you told we can vary 7, but hwat is 7 here, it is the order of
polynomial equation. not the value of 'n'.
Here, you have already define 'n', but in our case we dont know n
This is like trial and error, where-in, i should check each value of 'n' and find the best fit
Run a loop with n.

Sign in to comment.

madhan ravi
madhan ravi on 15 Nov 2018
Edited: madhan ravi on 15 Nov 2018
use interp1() with appropriate method:
x = linspace(-2*pi,2*pi,100) ;
xx=linspace(x(1),x(end),1000);
y = sin(x).^7 ;
yy=interp1(x,y,x,'spline')
figure
plot(x,y,'o',x,yy,'r')

1 Comment

Megha
Megha on 15 Nov 2018
Edited: Megha on 15 Nov 2018
I would like to mention i do not want to polyfit the equation.
I have a dataset, Y and angles in X. There is a nearly type of distribution.
So i want to fit the dataset with eqn for different "n" such that i can find best fit.

Sign in to comment.

Image Analyst
Image Analyst on 15 Nov 2018
My favorite fitting function is fitnlm() in the Statistics and Machine Learning toolbox. It could easily find this n. Just attach your data in a .mat file and I can do it for you. You have a non-linear equation in variable n. You could turn it into a linear equation in n by taking the log of the data and then using polyfit, but do you know how that affects the accuracy? Why not use fitnlm() to fit a non-linear model to n right from the start?

4 Comments

Torsten
Torsten on 15 Nov 2018
Edited: Torsten on 15 Nov 2018
I think it's not advisable to use a fitting tool since sin(x)^n is complex-valued for sin(x)<0 and n not being an integer.
You could just include a lower bound for n. It should be a non-zero integer anyway.
Torsten
Torsten on 15 Nov 2018
Edited: Torsten on 15 Nov 2018
sin(x)^a is complex-valued if sin(x)<0 and "a" not being an integer. How should this be healed by including a lower bound for "a" ?
Ah, yes, you are right. But as I already mentioned, it doesn't matter. The exponent should be an integer, so this will be a real value for any valid n.

Sign in to comment.

Categories

Asked:

on 15 Nov 2018

Commented:

Rik
on 15 Nov 2018

Community Treasure Hunt

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

Start Hunting!