matlabdatatablesignalscurve-fittingdata-fitting

MATLAB Invalid use of operator error message


The error lies in the line of

myfittype = fittype("A*cos(omega_0*t+phi)",dependent="A" "omega_0" "phi",independent="t",coefficients=["A" "omega_0" "phi"]);

The following error message appears:

File: TOungedsurfacefit.m Line: 15 Column: 58
Invalid use of operator.

I added a semicolon as suggested in the line error fix message, but it didn't change anything the datatype of s & t is doubles & they are both 400x1 matrices aka vertically imported column vectors in the workspace. Also I saw, that there was a missing closure bracket, but I added that too & still the error occurred. How to proceed? I would be grateful for any further help! I am a beginner in matlab and dont know anything unfortunately.

    p=pi;
    T= 3.95;
    omega_0 = (2*p)/T;
    fo = fitoptions('Method','NonlinearLeastSquares','Lower',[-500,500],'Upper',[Inf,max(s)],'StartPoint',[-500,500]);
     %alternative:fo = fitoptions('Method','NonlinearLeastSquares','Lower',[-500,500],'Upper',[Inf,max(s)],'StartPoint',[-500,500]);   
    x = t;  
    y = s;  
    myfittype = fittype("A*cos(omega_0*t+phi)",dependent="A" "omega_0" "phi",independent="t",coefficients=["A" "omega_0" "phi"]);
    myfit = fit(t',myfittype);
    plot(myfit,t);
    xdft = fft(t); 
    camp = 2/length(t)*xdft(t);     
    phi = angle(camp); 
    A= abs(camp);

In general, t is the x value & s is the y value accordingly. I have imported the data table as column vectors.


Solution

  • You are using MATLAB's named arguments and string types, which are not the most standard use of MATLAB (not your fault, you are using the docs).

    The correct syntax is:

    myfittype = fittype("A*cos(omega_0*t+phi)",dependent=["A", "omega_0", "phi"],independent="t",coefficients=["A", "omega_0", "phi"]);
    

    When in doubt, always add the commas and brackets around arrays.

    This still errors because you can't use A for both a coefficient and a dependent variable.