Crisp
Crisp

Reputation: 257

Plotting two different equations on the same graph/matlab

While I am able to plot my FFT( fast fourier transform) plot(X,Y), however I am unable to plot my fit line f(x) along with my FFT. The equations was gathered from the curve fitting tool, I took the ten best fit and averaged the to come up with equation f(x).

what must I do for f(x) to be plotted with log(freq) and log(Pow)

code:

row=69;
col=69;

colormap gray
whitebg('black')

iterations=10^3;


Next=zeros(row,col);
laplacian=zeros(row,col);
critical=zeros(row,col);

B= zeros(row,col);
lums=zeros(1000);


flw=0.5;

u=0.1;

crit=5;
%bns=200;
bns=1000;

for k=1:iterations
    B=B+(rand(row,col)-0.5);
   Next=B;

rns=5.;
for i=1:row
for j=1:col

rfromns=(col+rns-j);
        critical(i,j)=0;
        if i<=2 left=row; else left=(i-1);end
        if i==row right=1; else right=(i+1);end
        if (j<=2) top=1; else top=(j-1);end
        if (j==col) bottom=j; else bottom=(j+1);end
        l=B(i,top)+B(left,j)+B(right,j)+B(i,bottom)+0.5*(B(left,top)+B(right,top)+B(left,bottom)+B(right,bottom));
        l=l-6.*B(i,j);
        laplacian(i,j)=l;
        bfromns=bns/rfromns^3;
        if (abs(l)/((abs(B(i,j))+bfromns)+0.01))>crit; critical(i,j)=1; end
        %if abs(l)>crit; critical(i,j)=1; end
end
end



            for j = 1:col
            if (j==col) lum=0.;end
            for i = 1:row

            if (j>1) Next(i,j)=(1-flw)*B(i,j)+flw*B(i,j-1); end;
            if (j==1) Next(i,j)=(1-flw)*B(i,j); end;

            if (critical(i,j)==1)&& (j>1)   Next(i,j)=B(i,j)*(1-flw-flw*u)+flw*B(i,j-1)+(flw*u)*B(i,j)/5.; end;
            if i<2 left=row; else left=(i-1);end
            if i==row right=1; else right=(i+1);end
            if (j<=2) top=1; else top=(j-1);end
            if (j==col) bottom=j; else bottom=(j+1);end

            if (critical(left,j)==1) Next(i,j)=Next(i,j)+flw*u*B(left,j)/5.;end
            if (critical(right,j)==1) Next(i,j)=Next(i,j)+flw*u*B(right,j)/5.;end
            if (critical(i,top)==1) Next(i,j)=Next(i,j)+flw*u*B(i,top)/5.;end
            if (critical(i,bottom)==1) Next(i,j)=Next(i,j)+flw*u*B(i,bottom)/5.;end

            if (j==col) lum=lum+u*B(i,j)*B(i,j)+abs(laplacian(i,j)); end
            end
            end

lums(k)=lum;

B=Next;                
%Matplot(B)
%if (k>00)
surf(B);
%plot(lums)
%view([0 90])
%pause(0.001)
%end
end


c=fft(lums(129:iterations));
pow=abs(c).^2;
pow=pow(2:(iterations-128)/2);
freq=(2:(iterations-128)/2);

X=log(freq);
Y=log(pow);

%x=length(X);

x=0.6:.1:6.;

%Linear model Poly2
a1 = -0.155;  
a2 = 0.2154;  
a3 = 15.1;
af(x) = a1*x.^2 + a2*x + a3;

%Linear model Poly3
b1 = 0.01805;  
b2 = -0.3687;  
b3 = 0.9874;  
b4 = 14.29; 
bf(x) = b1*x.^3 + b2*x.^2 + b3*x + b4;


%General model Power2
c1 = -0.09124;  
c2 = 2.179;  
c3 = 15.34;
cf(x) = c1*x.^c2+c3;


%General model Rat02
d1 = 727.3;  
d2 = -3.447;  
d3 = 51.6; 
df(x) = (d1) / (x.^2 + d2*x + d3);


%General model Gauss1
e1 = 15.01;  
e2 = 1.346;  
e3 = 8.152; 
ef(x) =  e1*exp(-((x-e2)/e3).^2);


%General model Gauss2 
w1 =  1.737;  
w2 =  3.46;
w3 =  2.333;  
w4 =  30.03;  
w5 =  -23.14;
w6 =  28.23;
wf(x) =  w1*exp(-((x-w2)/w3).^2) + w4*exp(-((x-w5)/w6).^2); 


%General model Sin1
g1 = 15.11;  
g2 = 0.1526;  
g3 = 1.428;  
gf(x) =  g1*sin(g2*x+g3);



%Linear model Poly4
h1 =  0.0179;  
h2 =  -0.252;  
h3 =  1.047;  
h4 =  -1.97; 
h5 =  16.23;  
hf(x) = h1*x.^4 + h2*x.^3 + h3*x.^2 + h4*x + h5;


%General model Fourier1
m1 =  11.05;  
m2 =  3.31;  
m3 =  2.104;  
m4 =  0.3644;  
mf(x) =  m1 + m2*cos(x*m4) + m3*sin(x*m4);


%Linear model
p1 =  0.815;
p2 =  0.1061;
p3 =  8.904;
pf(x) = p1*(sin(x-pi)) + p2*((x-10).^2) + p3;


f(x)=(af(x)+bf(x)+cf(x)+df(x)+ef(x)+wf(x)+gf(x)+hf(x)+mf(x)+pf(x))/10;



plot(X,Y)
plot(f(x))

Upvotes: 0

Views: 447

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

Matlab/Octave therefore use the hold keyword.

If you want to plot several things on one graph, you start your program with hold on, then execute one or more plot command, and finalize with hold off.

Example:

hold on;
x = -10:0.1:10;
plot (x, sin (x));
plot (x, cos (x));
hold off;

Documentation: https://www.gnu.org/software/octave/doc/interpreter/Manipulation-of-Plot-Windows.html


As the documentation describes, plot will normally call the newplot command, that removes the previous plot result, with hold on; such behavior is prevented.

Upvotes: 1

Related Questions