0
votes

I have a set of noisy data and want to fit a custom equation though it in MATLAB. Next I would take the values of the coefficients and utilize them in my algorithm. However I am stuck and I cant figure out why. I use a non linear equation a+b*log10(x1-dcos(alpha-x2)) where x1,x2 and the response value are known. First problem is the coefficients of a ,b, and alpha must be bounded. alpha here being in degrees can only vary from 0 to 360 for example.I dont know how to achieve this using curve fitting toolbox.

I have also tried other options like non linear regression techniques in MATLAB( fitnlm,lsqcurvefit etc) which proved to be disappointing as i cant have bounds over these variables. So in spite of fit being quite good, the coefficients are way too bad.

So, Question 1 : How do I fit multiple variables using curve fitting ? Question 2 : If thats not possible then what other techiniques can I use except non linear regression .

Many thnaks in advance ! Have a great day !

1
Configure your problem in cftool until you get it right, then use the "export code" function to learn how to do it programmatically.Dev-iL
@Sayantan Roy Did you try the answer? Or please clarify your questionanquegi
@anquegi Sorry for the late reply. Yes I saw your answer and thank you for the effort. My problem is the fitting that I get gives a weird alpha value which can only be between 0 and 360. I used your function and added a bound. However, the fitting doesnot meet my needs. I feel that a set of data may have different number of solutions since cos(theta) = cos(-theta). So as matlab computes the angle alpha it generates a value, however this alpha can be another value too which would satisfy the same equation and data set. I dont know how to get multiples values for alpha .Sayantan Roy
@Sayantan Roy, maybe you can use the tirgonmetic identities that I added later to the response, with this will we easy to find that value, also we carefull that x2 is also angleanquegi
and be carefully about cos(alfa), it is true cos(alfa) = cos(-alfa), but in this case is cos(-alfa+X2). so in my opinion alfa can only be alfa + n*360 being n from 0 to infinity, so is the same angle, be carefull with the range for X2anquegi

1 Answers

1
votes

Well If I get your problem you have a set of data, for the variables x1 and x2 and thre result y, and you want to model it with this equation:

y =  a + b * log10(x1 - cosd(alpha - x2)) % I suppose that dcos = cosd, I do not really known this functions

First I will create the data for this values:

function y = getting_data(x1,x2)

a = 3;
b = 5;
alpha = 120;

y =  a + b * log10(x1 - cosd(alpha - x2));

Now let's generate de datasets

>> % generate the data sets
>> x1 = 9 .* rand(1000,1) + 1; % random values [1,10]
>> x2 = 360 .* rand(1000,1); % random values [0,360]
>> y = getting_data(x1,x2); % the values for the function

create a function that use curve fitting for your model

function myfit = fitting_data(x1,x2,y)

myfittype = fittype('a + b * log10(x1 - cosd(alpha - x2))',...
    'dependent',{'y'},'independent',{'x1','x2'},...
    'coefficients',{'a','b','alpha'})

myfit = fit([x1 x2],y,myfittype)

be carefull with the input vector it should be nx1 to the fit function

and finally we get the coefficients:

>> fitting_data(x1,x2,y)

myfittype = 

     General model:
     myfittype(a,b,alpha,x1,x2) = a + b * log10(x1 - cosd(alpha - x2))
Warning: Start point not provided, choosing random start point. 
> In curvefit.attention.Warning/throw (line 30)
  In fit>iFit (line 299)
  In fit (line 108)
  In fitting_data (line 7) 

     General model:
     myfit(x1,x2) = a + b * log10(x1 - cosd(alpha - x2))
     Coefficients (with 95% confidence bounds):
       a =           3  (3, 3)
       b =           5  (5, 5)
       alpha =         120  (120, 120)


     General model:
     ans(x1,x2) = a + b * log10(x1 - cosd(alpha - x2))
     Coefficients (with 95% confidence bounds):
       a =           3  (3, 3)
       b =           5  (5, 5)
       alpha =         120  (120, 120)

wich represent the values that we guess

Also it will be usefull to separe de con(A - B) like this:

trigonometic identities

and also remember that

enter image description here