why the plot doesn't work ,in the code i'm finding the estimation value of the phase using montecarlo simulation
Show older comments
%phase astimation using monte carlo
clear all
close all
clc
%signal model x(n)=Acos(2pif0n+thetha)+w(n)
phase=0.75;
A=1;
sigma=0.1;
f0=0.035;
SNR=A^2/2*(sigma^2)
for m=1:length(SNR)
M=5;
for i=1:M;
s=A*cos(2*pi*f0*i+phase);
w=randn(size(s));
x(i)=s+sigma*w
y1=sum(x*sin(2*pi*f0*i))
y2=sum(x*cos(2*pi*f0*i))
phasehat(i)=-atan(y1+y2)
%
% mat(i,:)=phasehat
end
%compute bias
b(m)=(1/M)*sum(mean(phasehat(i))-phase)
%mean square error
mse=mean((phasehat(i)-phase)^2)
var=1/M*SNR
end
figure(1)
plot(10*log(SNR),b)
figure(2)
plot(10*log(SNR),mse)
figure(3)
plot(10*log(SNR),10*log(var))
Answers (1)
since length(SNR) is 1, you are running one loop iteration and generating one point. plot() doesn't show one point unless you specify a marker:
figure(1)
plot(10*log(SNR),b,'o')
figure(2)
plot(10*log(SNR),mse,'x')
figure(3)
plot(10*log(SNR),10*log(var),'^')
or equivalently, use scatter:
figure(1)
scatter(10*log(SNR),b)
% ...etc
4 Comments
Moving my answer here since Chris and I said the same thing (and used the same markerstyles!)
SNR and b, mse, and var are all scalars, meaning single numbers. The default plot setting in MATLAB only include a linestyle, not a markerstyle. When you have a single point, there is no line, so that is why your plots appear to be empty.
A=1;
sigma=0.1;
SNR=A^2/2*(sigma^2)
b = -0.3671;
mse = 3.3692;
var = 1.0000e-03;
figure(1)
plot(10*log(SNR),b,'o')
figure(2)
plot(10*log(SNR),mse,'x')
figure(3)
plot(10*log(SNR),10*log(var),'^')
Cris LaPierre
on 21 Jan 2022
I'd start by making SNR a vector of numbers rather than a single number.
I'd recommend going through MATLAB Onramp if you do not know what that means. I'd recommend at least the following chapters:
- Ch 4 - vectors and matrices
- Ch 5 - indexing into and modifying arrays
- Ch 6 - array calculations
- Ch 9 - plotting
- Ch 12 - programming (section 2 covers for loops)
mhamad Yaacoub
on 21 Jan 2022
mhamad Yaacoub
on 21 Jan 2022
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




