# Sinusoidal curve from discrete dataset

3 Ansichten (letzte 30 Tage)
Ashfaq Ahmed am 6 Mär. 2023
Kommentiert: William Rose am 17 Mai 2023
Hi!
I have a few discrete data-set and when I plot them it looks like this -
Can anyone please tell me how can I fit these discrete points into a sinusoidal curve like this?
The data is a small 23x4000 matrix and I added the .mat file to the question. Any feedback will be really helpful. Thank you!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

William Rose am 6 Mär. 2023
Bearbeitet: William Rose am 7 Mär. 2023
[edit: I changed the comments in the code for t1 and t. I added a line to display the fitted values.]
load('sinusoidal');
[rows,cols]=size(Sinusoidal); % read data form file
t1=(1:rows)'; % t1=vector
t=repmat(t1,1,cols); % t=array
Sinusoidal=reshape(Sinusoidal,rows*cols,1); % convert array to vector
T=Sinusoidal(~isnan(Sinusoidal)); % remove NaNs
t=t(~isnan(Sinusoidal)); % remove corresponding times
% next: define the model equation
myfittype=fittype('a*sin(w*t+p)','dependent',{'T'},'independent',{'t'},'coefficients',{'a','w','p'});
x0=[30,.1,0]; % initial guess for [a,b,c]
myfit=fit(t,T,myfittype,'StartPoint',x0); % fit the data
plot(myfit,'-b',t,T,'r.'); % plot results
xlabel('Time'); ylabel('Temperature')
% Display best-fit parameters on console.
fprintf('y=a*sin(wt+p): a=%.4f, w=%.4f, p=%.4f\n',myfit.a,myfit.w,myfit.p)
y=a*sin(wt+p): a=22.4003, w=0.1006, p=-0.1809
Try it.
##### 6 Kommentare4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
Ashfaq Ahmed am 16 Mai 2023
I was folowwing your code to make a sinusoidal signal from another temperature record. Your code works fine with the sinusoidal.mat file that I added to this question. But when I have a single array of temperature record, it does not work. I am getting this error message -
Error using fit>iFit (line 135)
X must be a matrix with one or two columns.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain,
fittypeobj, ...
Error in BuoyCheck (line 140)
myfit=fit(t,TEMP,myfittype,'StartPoint',x0); % fit the data
I added the .mat file. Can you please give me an idea on how can I get a very good fit sinusoidal curve from the temprature record? The reocrd is in T.Temp array.
I will really appreciate any feedback coming from you.
William Rose am 17 Mai 2023
@Ashfaq Ahmed, This is rather complicated to do properly, because the data in T.mat has values in datetime format, for the sample times. It also has the numeric values for time. Matlab is telling users to stop using numeric times, and start using datetime values. Therefore I am trying to adapt. The data in T.mat spans approximately 10 years, with uneven time intervals between samples (long data gaps every winter). Please contact me through Matab Central by clicking on my name above, then click on the envelope icon in the pop-up box. Please provide your email in the message, so we can discuss offline.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by