MATLAB Curve Fitting in MATLAB: Error Diagnosis

AI Thread Summary
The discussion revolves around fitting a model of the form asin(bx) to two 1D matrices, X and Y, using MATLAB's fittype function. The user encountered an error indicating that the independent variable x does not appear in the equation expression. The solution involves specifying the independent variable correctly in the fittype definition. By default, fittype uses 'x' as the variable name, so when using a different name like 'X', it is necessary to indicate this by using the 'independent' option in the fittype call. The corrected code should look like this: fit = fittype(@(a,b,X), a*sin(b*X), 'independent', {'X'}). This adjustment resolves the error and allows for successful fitting of the model.
ergospherical
Science Advisor
Homework Helper
Education Advisor
Insights Author
Messages
1,097
Reaction score
1,384
I have two 1D matrices X(1,j) and Y(1,j) of equal length. To fit Y to a model asin(bx) I tried:
Matlab:
fit = fittype(@(a,b,X), a*sin(b*X));
[fitted, gof] = fit(X, Y, fit)
coefficients = coeffvalues(fitted)
this gives the message: Error using fittype>iDeduceCoefficients
The independent variable x does not appear in the equation expression.
Use x in the expression or indicate another variable as the independent variable.


What's wrong?
 
Physics news on Phys.org
  • Like
Likes ergospherical
sweet, thanks
 
  • Like
Likes Wrichik Basu

Similar threads

Replies
1
Views
9K
Replies
1
Views
2K
Replies
6
Views
5K
Replies
12
Views
4K
Replies
4
Views
1K
Replies
5
Views
2K
Back
Top