Fitting function

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Fitting function

mrodrig
Hello,
I am new with octave.

I have a data set (x,y) and I have a theoretical function that depend on the
x value. In other words, for example, for x < b, f=10*x*a and for x>=b,
f=-10*x*a. I need to perform the fitting of the theoretical function to the
experimental data and obtain the value of R parameter.

Here is my code example

x=[1,2,3,4,5,6,7,8,9,10];
y=[0,2,6,10,12,10,7,5,1,0];
R=2;
N=1;
plot(x,y)
hold
  for r=min(x):1:max(x)
  if x(N) < 5
    y(N)=10*x(N)*R;
  elseif x(N) >= 5
   y(N)=-10*x(N)*R;
   endif
  N=N+1;
  endfor

I used R=2 as a starting parameter

How can I do?

It is possible to use leasqr?

thanks very much
 



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Fitting function

Doug Stewart-4


On Fri, Aug 9, 2019 at 10:31 PM mrodrig <[hidden email]> wrote:
Hello,
I am new with octave.

I have a data set (x,y) and I have a theoretical function that depend on the
x value. In other words, for example, for x < b, f=10*x*a and for x>=b,
f=-10*x*a. I need to perform the fitting of the theoretical function to the
experimental data and obtain the value of R parameter.

Here is my code example

x=[1,2,3,4,5,6,7,8,9,10];
y=[0,2,6,10,12,10,7,5,1,0];
R=2;
N=1;
plot(x,y)
hold
  for r=min(x):1:max(x)
  if x(N) < 5
    y(N)=10*x(N)*R;
  elseif x(N) >= 5
   y(N)=-10*x(N)*R;
   endif
  N=N+1;
  endfor

I used R=2 as a starting parameter

How can I do?

It is possible to use leasqr?

thanks very much




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



Your formula for the straight lines needs an y offset
I have a solution for you if you want it.
--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: Fitting function

mrodrig
Dear Doug,

Thanks very much for your replay. Please, could you send me the solution?





--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Fitting function

Doug Stewart-4


On Sat, Aug 10, 2019 at 8:01 AM mrodrig <[hidden email]> wrote:
Dear Doug,

Thanks very much for your replay. Please, could you send me the solution?


OK.
You are trying to fit 2 straight lines to a set of data. Straight lines are of the form y = a*x+b. You did not have the b.


first I wrote a M file function that calculated a least-square value and returned this value.
The input to this function has to be the unknowns your R and the 2  'b's.

 Then I wrote the calling function and used the fminsearch function like this:
w=fminsearch (@q1,[R;b1;b2])
w is the resulting values for R b1 b2

Fminsearch takes the starting values for R b1 b2 and adjusts them to minimize the value returned from q1.m

I then used the calculated values and calculated a resultant y3 values and then plotted them together.

If you have more questions I will try and answer them.
Let me know If this works for you.







--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html




--
DASCertificate for 206392




q1.m (380 bytes) Download Attachment
testq1.m (380 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fitting function

mrodrig
Dear Doug,

Thanks very much for your help. I will try and I will contact you again





--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html