Plotting two different equations on the same graph/matlab - 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))

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.

Related

polyfit and polyval funtions [duplicate]

This question already has an answer here:
MATLAB curve fitting - least squares method - wrong "fit" using high degrees
(1 answer)
Closed 3 years ago.
Hello my question is about curve tiffing of P3(x) I write my code with using polyfit and polyval functions but I need to make it without using these functions.
this is my code that I wrote
n=input('Enter a value: ');
if(n<3)
fprintf('Enter a different value larger than 3')
end
if(n>=3)
x = 1:n;
y = -0.3*x + 2*randn(1,n);
[p,S] = polyfit(x,y,3);
[y_fit,delta] = polyval(p,x,S);
plot(x,y,'bo')
hold on
plot(x,y_fit,'r-')
title('Linear-Fit Output')
legend('Data','Linear Fit')
end
this is code that I write it is working but I am suppose to write it without using polfite and polyval functions
Without Using syms
y = a0*x^0 + a1*x^1 + a2*x^2 + a3*x^3
For n data point --> y = X*a
where
X = [x1^0 , a1*x1^1, a2*x1^2 , a3*x1^3; x2^0 , a1*x2^1, a2*x2^2 , a3*x2^3;...;xn^0 , a1*xn^1 , a2*xn^2 , a3*xn^3 ]
and a = [a0, a1, a2, a3]; y = [y1, y2, ..., yn]
a is computed as follow
y = X*a ---> a = X\y
The code is as follows
n is given
x = 1:n;
y = -0.3*x + 2*randn(1,n);
x0 = ones(n, 1);
x1 = x';
x2 = (x.^2)';
x3 = (x.^3)';
X = [x0 x1 x2 x3];
a = X\(y');
f =#(t)a(1) + a(2).*t + a(3).*(t.^2)+ a(4).*(t.^3);
Use least square method to find best fit cubic polynomial
n=input('Enter a value: ');
if(n<3)
fprintf('Enter a different value larger than 3')
else
x = 1:n;
y = -0.3*x + 2*randn(1,n);
% Cubic regression
syms a0 a1 a2 a3
yq = a0 + a1.*x + a2.*(x.^2) + a3.*(x.^3) ;
rq = yq - y;
f = sum(rq.^2);
fa0 = diff(f,a0);
fa1 = diff(f,a1);
fa2 = diff(f,a2);
fa3 = diff(f,a3);
sol = solve(fa0 == 0, fa1 == 0, fa2 == 0, a0, a1, a2, a3);
a0 = sol.a0;
a1 = sol.a1;
a2 = sol.a2;
a3 = sol.a3;
% Cubic Regression Curve Function
f =#(t)a0 + a1.*t + a2.*(t.^2)+ a3.*(t.^3);
% Plot Data and Cubic Regression Curve
h = figure(1);
% Data
plot3 = scatter(x, y, 100, '+', 'MarkerEdgeColor', 'red', 'linewidth', 5);
hold on
% cubic Regression Curve
xx = linspace(0,n,100);
plot4 = plot(xx, f(xx), 'linewidth', 5);
[~,b] = legend([plot3 plot4],{'Real Data','Cubic Regression'}, 'FontSize',30);
set(findobj(b,'-property','MarkerSize'),'MarkerSize',30);
xlabel('x-axis','color', 'k', 'fontSize', 25)
ylabel('y-axis', 'color','k', 'fontSize', 25)
hYLabel = get(gca,'YLabel');
set(hYLabel,'rotation',0,'VerticalAlignment','middle', 'HorizontalAlignment','right')
grid on
grid minor
set(gca,'FontSize',20)
set(get(h,'CurrentAxes'),'GridAlpha',0.8,'MinorGridAlpha',0.5);
xticks(x);
title('Cubic Regression', 'color', 'r');
whitebg('w');
end
n = 5
n = 20

Confusion in MATLAB filter roll off

I have the following code in matlab:
L = 10000;
Power = -100;
A = 10^0.5;
Fs = 25e6;
fc1 = 100;
fc2 = 1e3;
fc3 = 10e3;
fc4 = 100e3;
fc5 = 1e6;
a1 = 2*pi*fc1/Fs;
a2 = 2*pi*fc2/Fs;
a3 = 2*pi*fc3/Fs;
a4 = 2*pi*fc4/Fs;
a5 = 2*pi*fc5/Fs;
x = wgn(1,L,Power);
y1 = zeros(1,L);
y2 = zeros(1,L);
y3 = zeros(1,L);
y4 = zeros(1,L);
y5 = zeros(1,L);
y = zeros(1,L);
for i = 2:L,
y1(i) = (1-a1)*y1(i-1) + a1*x(i);
y2(i) = (1-a2)*y2(i-1) + a2*x(i)/A;
y3(i) = (1-a2)*y3(i-1) + a3*x(i)/A^2;
y4(i) = (1-a2)*y4(i-1) + a4*x(i)/A^3;
y5(i) = (1-a2)*y5(i-1) + a5*x(i)/A^4;
y(i) = y1(i) + y2(i) + y3(i) + y4(i) + y5(i);
end
fft1 = fft(y);
fft1 = fft1(1:length(y)/2+1);
psd1 = (1/(Fs*length(y)))*abs(fft1).^2;
psd1(2:end-1) = 2*psd1(2:end-1);
freq = 0:Fs/length(y):Fs/2;
figure(3);
semilogx(freq,10*log10(psd1))
grid on
Ts = 40e-9;
z = tf('z',Ts);
H1 = a1/(1-(1-a1)*z^-1);
H2 = (a2/A)/(1-(1-a2)*z^-1);
H3 = (a3/A^2)/(1-(1-a3)*z^-1);
H4 = (a4/A^3)/(1-(1-a4)*z^-1);
H5 = (a5/A^4)/(1-(1-a5)*z^-1);
H = (H1 + H2 + H3 + H4 + H5);
figure(5);
bode(H),grid
The intent of this code is to model flicker noise, which has 10dB/dec of slope in its power spectral density.
To model that there is a filter whose output is y(i) and input x(i) which is white guassian noise in this case. In the bode plot of the filter (labelled as figure 5) I can see that it has 10dB/dec of roll-off as I intended.
But when I check the output noise (y(i) in this case) power spectral density (labelled as figure 3) I am seeing 20dB/dec of roll-off.
Could someone please explain what I did wrong and why I am not able to get the 10dB roll-off in power spectral density?
Your bode plot uses a transfer function which is the sum of 5 similar subfilters, each using a coefficient ai. In your implementation, you should similarly have 5 subfilters with a regular structure. Howerver, while copying each line you have left some of the coefficients for y3, y4 and y5 to use a2. You should get your expected result by simply substituting the respective a3, a4 and a5 like so:
for i = 2:L,
y1(i) = (1-a1)*y1(i-1) + a1*x(i);
y2(i) = (1-a2)*y2(i-1) + a2*x(i)/A;
y3(i) = (1-a3)*y3(i-1) + a3*x(i)/A^2;
y4(i) = (1-a4)*y4(i-1) + a4*x(i)/A^3;
y5(i) = (1-a5)*y5(i-1) + a5*x(i)/A^4;
y(i) = y1(i) + y2(i) + y3(i) + y4(i) + y5(i);
end

Matlab: Confusion in Discrete time filter design

I have the following code in matlab:
L = 10000;
PN_30dB = -100; % dBc per Hz at 10k
f_offset = 5e6;
f_offset2 = 10e5;
F0 = 2.5e9;
A = 10^0.5;
Fs = 25e6;
fc1 = 100;
fc2 = 1e3;
fc3 = 10e3;
fc4 = 100e3;
fc5 = 1e6;
a1 = 2*pi*fc1/Fs;
a2 = 2*pi*fc2/Fs;
a3 = 2*pi*fc3/Fs;
a4 = 2*pi*fc4/Fs;
a5 = 2*pi*fc5/Fs;
sigma3 = (f_offset2/F0)*sqrt(2*10^(PN_30dB/10)/F0);
y1 = zeros(1,L);
y2 = zeros(1,L);
y3 = zeros(1,L);
y4 = zeros(1,L);
y5 = zeros(1,L);
y = zeros(1,L);
x = zeros(1,L);
for i = 2:L,
x(i) = sigma3*randn(1);
y1(i) = (1-a1)*y1(i-1) + a1*x(i);
y2(i) = (1-a2)*y2(i-1) + a2*x(i)/A;
y3(i) = (1-a3)*y3(i-1) + a3*x(i)/A^2;
y4(i) = (1-a4)*y4(i-1) + a4*x(i)/A^3;
y5(i) = (1-a5)*y5(i-1) + a5*x(i)/A^4;
y(i) = y1(i) + y2(i) + y3(i) + y4(i) + y5(i);
end
fft1 = fft(y);
fft1 = fft1(1:length(y)/2+1);
psd1 = (1/(F0*length(y)))*abs(fft1).^2;
psd1(2:end-1) = 2*psd1(2:end-1);
freq = 0:F0/length(y):F0/2;
figure(3);
semilogx(freq,10*log10(psd1))
grid on
acc = 0;
actual_timestamps_3 = zeros(1,L);
flicker = zeros(1,L);
for i = 1:L,
acc = acc + y(i);
actual_timestamps_3(i) = i/F0 + acc;
flicker(i) = acc;
end
fft1 = fft(2*pi*F0*flicker);
fft1 = fft1(1:length(flicker)/2+1);
psd1 = (1/(F0*length(flicker)))*abs(fft1).^2;
psd1(2:end-1) = 2*psd1(2:end-1);
freq = 0:F0/length(flicker):F0/2;
figure(4);
semilogx(freq,10*log10(psd1))
grid on
In this code, I am trying to create an output signal called flicker which should have a 30dB/dec roll-off of its power spectral density.
For that I am accumulating a signal called y(i) (in the second for loop) which has a 10dB/dec roll-off as can be seen in figure 3 inside the code. Since accumulation should add another 20db/dec, I am expecting the flicker signal to have a 30dB/dec roll off.
Signal y(i) is the output of a discrete time filter implemented in first for loop.
But I am not seeing the expected roll-off (30dB/dec) for the flicker signal (in figure 4). The plot shows only 20dB/dec for the flicker signal. Could someone please explain what I am doing wrong?
EDIT
Figure 4 in the code is shown below:
As you estimate the power spectral density with the FFT, you are looking at the data (flicker) which is effectively multiplied by a long rectangular window. This window introduces some spectral leakage, and unfortunately the decay of this spectral leakage for rectangular windows is less than 20dB/decade. The leakage from the stronger frequency components is thus masking the decay you are trying to observe.
To avoid this, you should multiply your signal by a different window function. There are plenty to experiment with (which offer different tradeoffs), but for illustration purposes you could use a blackman window with:
fft1 = fft(2*pi*F0*flicker .* blackman(length(flicker))');

matlab mesh2d issue which cant be found

im trying to use mesh2d function according to a guide I read.
for some reason im getting all the time this issue:
Undefined function 'mesh2d' for input arguments of type 'double'.
Error in Try1 (line 88)
[p,t] = mesh2d(allnodes, alledges);
I install mesh2d , according to the guide here:
https://github.com/dengwirda/mesh2d
but for some reason im still getting this issue...
this is my code:(im adding the code so it whould be easier in case im missing something, instead il mark the bad part)
clf
file = 'pattern3';
P = imread('Pattern3.png');
P = P(400:3400, 400:3400);
P = 255 - P*6;
P = 1-im2bw(P);
Nmin = min(size(P));
P = P(1:Nmin, 1:Nmin);
[xg, yg] = meshgrid(1:Nmin, 1:Nmin);
P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
P = padarray(P, [1 1], 0);
CC = bwconncomp(P);
dtheta = pi/24;
theta = (-pi:dtheta:(pi-dtheta))';
nodeouter = [1.1*cos(theta) 1.1*sin(theta)];
Nnodes = length(nodeouter);
nodelist = (1:Nnodes)';
allnodes = nodeouter;
alledges = [nodelist , mod(nodelist, Nnodes)+1];
for n = 1:CC.NumObjects
%for n = 2:2
newP = zeros(size(P));
newP(CC.PixelIdxList{1,n}(:)) = 1;
newP = filter2(fspecial('average',5),newP);
C = contourc(newP,[0.2 0.2]);
C = C(:,2:end)';
C2 = dpsimplify(C,1);
m = 1;
while m <= length(C2(:,1))
if(C2(m,1) == 1 || C2(m,2) == 1)
C2(m,:) = [];
else
m = m + 1;
end
end
C2 = (C2 - Nmin/2)/(Nmin/2);
C = (C - Nmin/2)/(Nmin/2);
figure(1)
hold all
plot(C2(:,1), C2(:,2))
axis image xy
drawnow
nodeinner = C2;
Nnodeshole = length(nodeinner);
nodelist = (1:Nnodeshole)';
edgelist = [nodelist , mod(nodelist, Nnodeshole)+1];
edgelist = edgelist + Nnodes;
allnodes = [allnodes; nodeinner];
alledges = [alledges; edgelist];
Nnodes = Nnodes + Nnodeshole;
n
end
%%
hdata.fun = #(x,y) 0.05*(1 + ((x.^2 + y.^2)/a^2)).^2;
[p,t] = mesh2d(allnodes, alledges); %%here is the issue!!!!!!!!!!!!!!!!!!!!!!!1
%%
as = 0.5;
for n = 1:length(as)
a = as(n);
h = 0;
x = p(:,1);
y = p(:,2);
z = zeros(size(x));
r = sqrt(x.^2 + y.^2);
phi = atan2(y,x);
theta = atan(r/(a+h));
alpha = 2*theta;
xnew = a*sin(alpha).*cos(phi);
ynew = a*sin(alpha).*sin(phi);
znew = -a*cos(alpha);
p2 = [xnew, ynew, znew];
stlwrite('Test.stl', t, p2)
fv.faces = t;
fv.vertices = p2;
clf
figure(3)
patch(fv, 'FaceColor', [1 1 1], 'EdgeColor', 'black', 'LineWidth', 0.1)
axis equal
axis off
xlim([-a a])
ylim([-a a])
zlim([-a a])
camlight head
view(58,28)
zoom(1.5)
drawnow
end
the photo im trying to use:
I recently completely rewrote MESH2D -- bringing it up-to-date with more recent meshing techniques and MATLAB functionality. It looks like you are trying to use subroutines from old versions of the library.
Based on the updates, the routines you want are refine2 and smooth2 (they build and then optimise a two-dimensional constrained Delaunay triangulation).
I recommend that you have a look at the example code contained in tridemo to see how the updated MESH2D toolbox works.

Plot of two variables varied simultaneously, on x-axis

My following code generates a graph with a looping variable for the x-axis. Specifically, eta_22 varies from 0 to 1, with loop iteration size of 0.01.
The code below the line is the source function file.
My question is: How can I generate a graph with eta_1 varying from 0 to 1, with loop iteration size of 0.01 as well? (I want a plot of AA on the y-axis, and eta_1, eta_2 varying from 0 to 1.)
My attempts: I have tried to create nested "for" loops, but the plot itself is looping. I have tried to put the plot line outside of the "for" loops as well, but that did not work.
Thanks for any help.
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
alpha = 100;
TIME = 365;
eta_22 = zeros(1,alpha);
AA = zeros(1,alpha);
for m = 1:1:alpha
eta_2 = m./alpha;
eta_22(m) = m./alpha;
Lambda = 531062;
mu = (1/70)/365;
mu_A = 0.25/365;
mu_T = 0.2/365;
beta = 0.187/365;
tau = 4/365;
lambda_T = 0.1;
rho_1 = 1/60;
rho_2 = (rho_1)./(270.*rho_1-1);
gamma = 1e-3;
eta_1 = 0;
S0 = 191564208;
T0 = 131533276;
H0 = 2405659;
C0 = 1805024;
C10 = 1000000;
C20 = 1000000;
CT10 = 500000;
CT20 = 500000;
y0 = [S0, T0, H0, C0, C10, C20, CT10, CT20];
[t,y] = ode45('SimplifiedEqns',[0:1:TIME],y0);
S = y(:,1);
T = y(:,2);
H = y(:,3);
C = y(:,4);
C1 = y(:,5);
C2 = y(:,6);
CT1 = y(:,7);
CT2 = y(:,8);
N = S + T + H + C + C1 + C2 + CT1 + CT2;
HIVinf1=[0:1:TIME];
HIVinf2=[beta.*(S+T).*(C1+C2)./N];
HIVinf=trapz(HIVinf1,HIVinf2);
AA(m) = HIVinf;
end
plot(100.*eta_22,AA./1000)
_____________________________________________________________________________________________________
function ydot = SimplifiedEqns(t,y)
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
S = y(1);
T = y(2);
H = y(3);
C = y(4);
C1 = y(5);
C2 = y(6);
CT1 = y(7);
CT2 = y(8);
N = S + T + H + C + C1 + C2 + CT1 + CT2;
ydot = zeros(8,1);
ydot(1)=Lambda-mu.*S-beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(S./N);
ydot(2)=tau.*(T+C).*(S./N)-beta.*(H+C+C1+C2).*(T./N)-(mu+mu_T).*T;
ydot(3)=beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(H./N)-(mu+mu_A).*H;
ydot(4)=beta.*(H+C+C1+C2).*(T./N)+tau.*(T+C).*(H./N)-(mu+mu_A+mu_T+lambda_T).*C;
ydot(5)=lambda_T.*C-(mu+mu_A+rho_1+eta_1).*C1;
ydot(6)=rho_1.*C1-(mu+mu_A+rho_2+eta_2).*C2;
ydot(7)=eta_1.*C1-(mu+rho_1+gamma).*CT1;
ydot(8)=eta_2.*C2-(mu+rho_2+gamma.*(rho_1)./(rho_1+rho_2)).*CT2+(rho_1).*CT1;
end
The simplest way:
eta_1=0:1/alpha:1;
eta_2=0:1/alpha:1;
lsize=length(eta_1) % I assume eta_1 and eta_2 are of the same size
for i=1:lsize
%Update your AA(i) here
end
plot(eta_1,AA,eta_2,AA)