non linear regression code

I did linear regression by the code below. data set is attached.
clear;
clc
load('data.mat');
F=F'; T=T'; W=W';
Q1=F;
Q2=T;
X=[Q1 Q2];
for i=1:70
Y=W(:,i);
% b = regress(Y,X,0.05);
mdl{i} = fitlm(X,Y);
XX{i}= mdl{i}.Coefficients;
rmqs(i)=mdl{i}.RMSE;
for j=1:3
b(i,j)=XX{i}{j,1};
er(i,j)=XX{i}{j,2};
end
end
I want to change this code to fit the data for a non-linear model. which is,
Y~b0+b1*Q1+b2*Q2+b3*cos(2*pi*T+b4)
instead,
Y~b0+b1*Q1+b2*Q2
I need the coefficients with errors. How can I do that?

Answers (1)

If you have a late enough version you can use fitnlm or nlinfit. I usually use the former.
Is T a predictor variable in this equation or a constant? If it is a constant you would want to create a function handle (which nlinfit requires anyhow, but fitnlm accepts). If it is a predictor you would need to place it in the matrix X and you can pass the modelspec in fitnlm as:
modelspec = 'y~b1+b2*x1+b3*x2+b4*cos(2*pi*x3+b5)';
I imagine you would want to estimate the frequency as well, unless this is known a priori.

4 Comments

I have 2015a. So I think it should work. Anyway if you see my previous code you see that T=Q2; I tried to use fitnlm but I don't know how to choose the initial values.
Y~b0+b1*Q1+b2*Q2+b3*cos(2*pi*Q2+b4)
T=Q2
The initial values will be the hardest part, and the results can vary considerably based upon this choice. Your best bet is to plot the data and take some close guesses. The intercept should be easiest, but then you can look at the amplitude and phase shift of the sinusoid (maybe reduce 2 only Q2-Y plane would help, and may make the slope in this direction more evident). Then plot the Q1-Y plane and this should help with the last slope.
Actually I can find the things that you said by googling. I want an example similar to my problem or the solution. I am not too much familiar with MATLAB regression. Any help will be appreciated.
As useful as google sounds like a complement, thank you. I might have to place that on my resume.

Sign in to comment.

Asked:

on 12 Aug 2015

Commented:

on 13 Aug 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!