Why does polyfit not work?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
Hi everyone,
I'm a high school student who has no experience with Matlab whatsoever. I'm working on a project that involves using Fourier analysis to predict the price of an asset (it's a little sketch). Unfortunately, it's due tomorrow and involves a presentation. Since it's a math class, I can explain the math, but the actual plotting on Matlab is killing me. Basically I'm copying the code from https://web.wpi.edu/Pubs/E-project/Available/E-project-022808-142909/unrestricted/FullIQPReport7.pdf
My initial file (LumensP.txt) is 365x1. The problem with the code is on line 9, right below the annotation "&& finding the trend." It also says pp1 is undefined, and there are other errors I can't identify on 15, 44, 29.
y=dlmread('LumensP.txt');
for i=1:365
x(i)=i;
end
L=length(x);
figure(1)
plot(x,y); hold on;
%%finding the trend
p1=polyfit(x,y,3);
xx=(1:1:365)';
pp1=polyval(p1,xx);
[m,~]=size(y);
[q,w]=size(pp1);
%%difference between trend and data
difference=pp1-y;
Y=fft(difference);
[x,z]=size(Y);
for k=8:365 Y1=Y; Y(k)=0; end P=ifft(Y); NewTrend=(abs(P)) + pp1; figure(1) plot(NewTrend); hold on; %% interpolating data for 3 months for k=366:458 t(k)= p1(1)*k^3 + p2(2)*k^2 + p1(3)*k+ p1(4); end %% to interpolate FFT curve for n=366:458 sum=0; for k=1:7 a(k)=real(Y1(k)); b(k)=-imag(Y1(k)); comp_k=(a(k)*cos((2*pi*(k-1)*(n-1))/L) + b(k)*sin((2*pi*(k-1)*(n-1))/L)); sum=(sum+comp_k)/L; end g(n)=sum; end figure(3) plot(abs(g),'r') %% predicting 3 month data NewTrend2 = t + g; monthdata=dlmread('LumensP.txt'); figure(1) plot(monthdata, 'g'); plot(NewTrend2) end
1 Comment
Walter Roberson
on 25 May 2018
Duplicates earlier and more complete https://www.mathworks.com/matlabcentral/answers/402498-why-does-polyfit-not-work
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!