how can i find a fuction which define input and output of a system?

i have a system and i put some values in and get its output. for instance y=f(x) i know the values of y and x .i get y by monte carlo simaltion numerically now ,how can i find a function which is close to f ? what command can i use? i need to use the function f in another m.file

 Accepted Answer

Use:
p = polyfit(x,y,n) % n is degree of polynomial, use a value that best fit your data
To check the fitting:
y_fit = polyval(p,x);
plot(x,y, x,y_fit)
If you are satisfied with your fitting, then your function will be:
f = p(1) + p(2)*x + p(3)*x.^2 + p(4)*x.^3 + ... + p(n+1)*x.^n
And, you can write this function in a separate m-file

4 Comments

thanks u aloooot mr rahman, it works but there is a problem the difference between two curves in some values is rather large and it is impossible to increase the n more than 13 and this difference probably have destructive result in the evaluation of system's performance due to its iterative structure. is there alternative way in your mind? i found out Curve fitting toolbox like cftool but it doesnt give me the function as well as your way( f = p(1) + p(2)*x + p(3)*x.^2 + p(4)*x.^3 + ... + p(n+1)*x.^n)
Yes, the performance of this regression model depends on the variability of data. However, you can use the curve fitting toolbox.
If you choose the fit as polynomial, then you will get nothing but what I wrote in f. So, first plot your data (plot(x,y)) and guess a trend, like linear, quadratic, sinusoidal, exponential, mixed etc.
Then, in the curve fitting toolbox select your y and x data, and choose fit as Custom Equation. Then write the approximation equation with some coefficients from your guess from the plot of actual data. The value of coefficients will be shown in result section on same window.
By the way, I am talking about an app in Matlab. Click on Apps menu, then open Curve Fitting.
You would never fit a 13th order polynomial. Have you ever seen what happens between and after your training points when you do that? They go haywire with enormous oscillations!
You should have some idea of the type of function you want to fit if you're looking for an analytical formula, like a polynomial or something. I have no idea what it might be because you're not sharing a plot of your data, or even the data itself so we're left to guess (or not even bother to answer which I'm sure many did).
I'm really appreciated you Mr. Rahman, its result is better when I use fitting as custom equation.
But my system is a receiver including 2 parts, elementary detector and a decoder. I want to find the function f between these parts. Its curve is like image below.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!