Reputation: 134
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 !
Upvotes: 0
Views: 1946
Reputation: 11522
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:
and also remember that
Upvotes: 1