MSE vs SNR in MATLAB, Matrix 7*1 - matlab

n=50;
x=2;
snr = -5:5:25;
zi = zeros(7,50);
mu = 1;
for i=1:length(snr)
sigma(1,i) = sqrt(1/(10^(snr(1,i)/10)));
%Let wi be gaussian ditribution with mean mu= 1 and sigma found above
wi = normrnd(mu,sigma(1,i),[1,n]);
zi(i,:) = x + wi;
end
I found zi using the above code but the matrix comes is 7 * 50. what I assume from the question is the answer should be 7 * 1 matrix. Can anyone help me please? Thank you

function check3
clc
n=1;
x=2;
snr = -5:5:25;
zi = zeros(7,1);
mu = 1;
for i=1:length(snr)
sigma(1,i) = sqrt(1/(10^(snr(1,i)/10)));
wi = normrnd(mu,sigma(1,i),[1,n]);
zi(i,:) = x + wi;
end
zi
Explanation
I have simply replaced 50 by 1 in the original code. Thank you.

Related

Negative values obtained in the solution of the 1D advection-dispersion equation using FD method

I am trying to solve the 1D ADE
This is my code so far:
clc; clear; close all
%Input parameters
Ao = 1; %Initial value
L = 0.08; %Column length [m]
nx = 40; %spatial gridpoints
dx = L/nx; %Length step size [m]
T = 20/24; %End time [days]
nt = 100; %temporal gridpoints
dt = T/nt; %Time step size [days]
Vel = dx/dt; %Velocity in each cell [m/day]
alpha = 0.002; %Dispersivity [m]
De = alpha*Vel; % Dispersion coeff. [m2/day]
%Gridblocks
x = 0:dx:L;
t = 0:dt:T;
%Initial and boundary conditions
f = #(x) x; % initial cond.
% boundary conditions
g1 = #(t) Ao;
g2 = #(t) 0;
%Initialization
A = zeros(nx+1, nt+1);
A(:,1) = f(x);
A(1,:) = g1(t);
gamma = dt/(dx^2);
beta = dt/dx;
% Implementation of the explicit method
for j= 1:nt-1 % Time Loop
for i= 2:nx-1 % Space Loop
A(i,j+1) = (A(i-1,j))*(Vel*beta + De*gamma)...
+ A(i,j)*(1-2*De*gamma-Vel*beta) + A(i+1,j)*(De*gamma);
end
% Insert boundary conditions for i = 1 and i = N
A(2,j+1) = A(1,j)*(Vel*beta + De*gamma) + A(2,j)*(1-2*De*gamma-Vel*beta) + A(3,j)*(De*gamma);
A(nx,j+1) = A(nx-1,j)*(Vel*beta + 2*De*gamma) + A(nx,j)*(1-2*De*gamma-Vel*beta)+ (2*De*gamma*dx*g2(t));
end
figure
plot(t, A(end,:), 'r*', 'MarkerSize', 2)
title('A Vs time profile (Using FDM)')
xlabel('t'),ylabel('A')
Now, I have been able to solve the problem using MATLAB’s pdepe function (see plot), but I am trying to compare the result with the finite difference method (implemented in the code above). I am however getting negative values of the dependent variable, but I am not sure what exactly I could be doing wrong. I therefore will really appreciate if anyone can help me out here. Many thanks in anticipation.
PS: I can post the code I used for the pdepe if anyone would like to see it.

spectral solution to 1D KdV equation

I am trying to solve the solution of a 1D KdV equation (ut+uux+uxxx=0) starting from a two solitons initial condition using Fourier spectral method. My code blows up the solution for a reason that I cannot figure out. I would appreciate any help. This is my main code file:
%Spatial variable on x direction
L=4; %domain on x
delta=0.05; %spatial step size
xmin=-L; %minimum boundary
xmax=L; %maximum boundary
N=(xmax-xmin)/delta; %number of spatial points
x=linspace(xmin,xmax,N); %spatial vector
% 1D Initial state
sigma1 = 2; sigma2 = 4;
c1 = 2; c2 = 1;
h1 = 1; h2 = 0.3;
U = h1*sech(sigma2*(x+c1)).^2+h2*sech(sigma1*(x-c2)).^2;
%Fast Fourier Transform to the initial condition
Ut = fftshift(fft(U));
Ut = reshape(Ut,N,1);
%1D Wave vector disretisation
k = (2*pi/L)*[0:(N/2-1) (-N/2):-1];
k(1) = 10^(-6);
k = fftshift(k);
k = reshape(k,N,1);
%first derivative (advection)
duhat = 1i *k .*Ut;
du = real(ifft(ifftshift(duhat))); %inverse of FT
%third derivative (diffusion)
ddduhat = -1i * (k.^3) .*Ut;
dddu = real(ifft(ifftshift(ddduhat))); %inverse of FT
% Time variable
dt = 0.1; %time step
tspan = [0 4];
%solve
Time = 50;
for TimeIteration = 1:2:Time
t= TimeIteration * dt;
[Time,Sol] = ode45('FFT_rhs_1D',tspan,U,[],du,dddu);
Sol = Sol(TimeIteration,:);
%plotting
plot(x,abs(Sol),'b','LineWidth',2);
end
And this is the function that solves the equation:
function rhs = FFT_rhs_1D(tspan,U,dummy,du,dddu)
%solve the right hand side
rhs = - U .* du - dddu;
end
Thank you.

Plotting function with a summation produces a wrong result

I have an equation that needs to be plotted, and the plot is coming out incorrectly.
The equation is as follows:
And the plot should look like this:
But my code:
clear; clc; close all;
eta = 376.7303134617706554679; % 120pi
ka = 4;
N = 24;
coeff = (2)/(pi*eta*ka);
Jz = 0;
theta = [0;0.0351015938948580;0.0702031877897160;0.105304781684574;0.140406375579432;0.175507969474290;0.210609563369148;0.245711157264006;0.280812751158864;0.315914345053722;0.351015938948580;0.386117532843438;0.421219126738296;0.456320720633154;0.491422314528012;0.526523908422870;0.561625502317728;0.596727096212586;0.631828690107444;0.666930284002302;0.702031877897160;0.737133471792019;0.772235065686877;0.807336659581734;0.842438253476592;0.877539847371451;0.912641441266309;0.947743035161167;0.982844629056025;1.01794622295088;1.05304781684574;1.08814941074060;1.12325100463546;1.15835259853031;1.19345419242517;1.22855578632003;1.26365738021489;1.29875897410975;1.33386056800460;1.36896216189946;1.40406375579432;1.43916534968918;1.47426694358404;1.50936853747890;1.54447013137375;1.57957172526861;1.61467331916347;1.64977491305833;1.68487650695319;1.71997810084804;1.75507969474290;1.79018128863776;1.82528288253262;1.86038447642748;1.89548607032233;1.93058766421719;1.96568925811205;2.00079085200691;2.03589244590177;2.07099403979662;2.10609563369148;2.14119722758634;2.17629882148120;2.21140041537606;2.24650200927091;2.28160360316577;2.31670519706063;2.35180679095549;2.38690838485035;2.42200997874520;2.45711157264006;2.49221316653492;2.52731476042978;2.56241635432464;2.59751794821949;2.63261954211435;2.66772113600921;2.70282272990407;2.73792432379893;2.77302591769378;2.80812751158864;2.84322910548350;2.87833069937836;2.91343229327322;2.94853388716807;2.98363548106293;3.01873707495779;3.05383866885265;3.08894026274751;3.12404185664236;-3.12404185664236;-3.08894026274751;-3.05383866885265;-3.01873707495779;-2.98363548106293;-2.94853388716807;-2.91343229327322;-2.87833069937836;-2.84322910548350;-2.80812751158864;-2.77302591769378;-2.73792432379893;-2.70282272990407;-2.66772113600921;-2.63261954211435;-2.59751794821949;-2.56241635432464;-2.52731476042978;-2.49221316653492;-2.45711157264006;-2.42200997874520;-2.38690838485035;-2.35180679095549;-2.31670519706063;-2.28160360316577;-2.24650200927091;-2.21140041537605;-2.17629882148120;-2.14119722758634;-2.10609563369148;-2.07099403979662;-2.03589244590177;-2.00079085200691;-1.96568925811205;-1.93058766421719;-1.89548607032233;-1.86038447642748;-1.82528288253262;-1.79018128863776;-1.75507969474290;-1.71997810084804;-1.68487650695319;-1.64977491305833;-1.61467331916347;-1.57957172526861;-1.54447013137375;-1.50936853747890;-1.47426694358404;-1.43916534968918;-1.40406375579432;-1.36896216189946;-1.33386056800461;-1.29875897410975;-1.26365738021489;-1.22855578632003;-1.19345419242517;-1.15835259853032;-1.12325100463546;-1.08814941074060;-1.05304781684574;-1.01794622295088;-0.982844629056025;-0.947743035161167;-0.912641441266309;-0.877539847371451;-0.842438253476592;-0.807336659581735;-0.772235065686877;-0.737133471792019;-0.702031877897161;-0.666930284002303;-0.631828690107445;-0.596727096212586;-0.561625502317728;-0.526523908422871;-0.491422314528013;-0.456320720633154;-0.421219126738296;-0.386117532843439;-0.351015938948581;-0.315914345053722;-0.280812751158864;-0.245711157264007;-0.210609563369149;-0.175507969474290;-0.140406375579432;-0.105304781684575;-0.0702031877897167;-0.0351015938948580;-2.44929359829471e-16];
for n = 0:N
if n == 0
kappa = 1;
else
kappa = 2;
end
num = (-1.^(n)).*(1i.^(n)).*(cos(n.*theta)).*(kappa);
Hankel = besselh(n,2,ka);
Jz = Jz + ((num./Hankel));
end
Jz = Jz.*coeff;
x = linspace(0,2*pi,length(theta));
plot(x,abs(Jz));
Produces the following incorrect plot:
Note that the values of theta are discrete angles around a circular cylinder.
The equation is the analytical solution to the current density for a TMz polarized cylinder in 2D.
I think that your result is actually correct and this is a simple problem with plotting or with how you specify theta. Since this is a periodic function, lets draw a few more periods:
function q52693512
eta = 376.7303134617706554679; % 120pi
ka = 4;
N = 24;
coeff = (2)/(pi*eta*ka);
Jz = 0;
theta = linspace(-3*pi, 3*pi, 180);
for n = 0:N
kappa = 1 + (n>0);
num = (-1.^(n)).*(1i.^(n)).*(cos(n.*theta)).*(kappa);
Hankel = besselh(n,2,ka);
Jz = Jz + ((num./Hankel));
end
Jz = Jz.*coeff;
figure(); plot(theta, abs(Jz));
You might already be able to see that the desired results is in there but shifted by half a period with respect to our result. This is clearer if we look again at the center (it's exactly the shape you want, if ignoring the horizontal axis values).
Try looking for some justification for ϕ being equal to theta ± π/2 (or something like that).

Implement finite difference method in matlab

I am trying to implement the finite difference method in matlab. I did some calculations and I got that y(i) is a function of y(i-1) and y(i+1), when I know y(1) and y(n+1). However, I don't know how I can implement this so the values of y are updated the right way. I tried using 2 fors, but it's not going to work that way.
EDIT
This is the script and the result isn't right
n = 10;
m = n+1;
h = 1/m;
x = 0:h:1;
y = zeros(m+1,1);
y(1) = 4;
y(m+1) = 6;
s = y;
for i=2:m
y(i) = y(i-1)*(-1+(-2)*h)+h*h*x(i)*exp(2*x(i));
end
for i=m:-1:2
y(i) = (y(i) + (y(i+1)*(2*h-1)))/(3*h*h-2);
end
The equation is:
y''(x) - 4y'(x) + 3y(x) = x * e ^ (2x),
y(0) = 4,
y(1) = 6
Thanks.
Consider the following code. The central differential quotient is discretized.
% Second order diff. equ.
% y'' - 4*y' + 3*y = x*exp(2*x)
% (y(i+1)-2*y(i)+y(i-1))/h^2-4*(y(i+1)-y(i-1))/(2*h) + 3*y(i) = x(i)*exp(2*x(i));
The solution region is specified.
x = (0:0.01:1)'; % Solution region
h = min(diff(x)); % distance
As said in my comment, using this method, all points have to be solved simultaneously. Therefore, above numerical approximation of the equation is transformed in a linear system of euqations.
% System of equations
% Matrix of coefficients
A = zeros(length(x));
A(1,1) = 1; % known solu for first point
A(end,end) = 1; % known solu for last point
% y(i) y'' y
A(2:end-1,2:end-1) = A(2:end-1,2:end-1)+diag(repmat(-2/h^2+3,[length(x)-2 1]));
% y(i-1) y'' -4*y'
A(1:end-1,1:end-1) = A(1:end-1,1:end-1)+diag(repmat(1/h^2+4/(2*h),[length(x)-2 1]),-1);
% y(i+1) y'' -4*y'
A(2:end,2:end) = A(2:end,2:end)+diag(repmat(1/h^2-4/(2*h),[length(x)-2 1]),+1);
With the rhs of the differential equation. Note that the known values are calculated by 1 in the matrix and the actual value in the solution vector.
Y = x.*exp(2*x);
Y(1) = 4; % known solu for first point
Y(end) = 6; % known solu for last point
y = A\Y;
Having an equation to approximate the first order derivative (see above) you can verify the solution. (note, ddx2 is an own function)
f1 = ddx2(x,y); % first derivative (own function)
f2 = ddx2(x,f1); % second derivative (own function)
figure;
plot(x,y);
saveas(gcf,'solu1','png');
figure;
plot(x,f2-4*f1+3*y,x,x.*exp(2*x),'ko');
ylim([0 10]);
legend('lhs','rhs','Location','nw');
saveas(gcf,'solu2','png');
I hope the solution shown below is correct.

Estimated mean and covariance calculation in matlab using maximum likelihood method

I am trying to calculate estimated mean and co-variance using maximum likelihood method in matlab. I am newbie in Matlab and having problems which i like to be cleared here.
I am using following code:
clear all;
%Visualization of 2D Gaussian Distribution
% Mean of the distribution
mu = [1 -1];
% Covariance matrix (Must be symetric)
sigma = [ 2 1 ; 1 3 ];
% Samples
X = mvnrnd(mu,sigma,1000);
analytical_mean = mean(X);
analytical_cov = cov(X);
N = size(X,1);
estimated_mean = sum(X)/N;
summation = 0;
for i=1:N,
row = X(i,:);
tmp1= (row - estimated_mean);
tmp2 = tmp1';
summation = summation + tmp2;
end
covar = summation/N;
Now analytical_mean and estimated_mean are coming equal but my calculated co-variance covar is not coming as a matrix like analytical_cov. Kindly I need to know how to calculate covar correctly.
I am using below equations:
you can try this instead
[m,n] = size(X);
estimated_mean = sum(X)/m;
tmp=zeros(m,n);
for i=1:n
tmp(:,i)= ((X(:,i) - estimated_mean(i)));
end
covar = (tmp.'*tmp)/m;
I think you want
tmp2 = tmp1'*tmp1;
instead of
tmp2 = tmp1'
That change makes covar pretty close for me:
covar =
1.9042 0.9534
0.9534 3.0195
The clue was the dimensions of covar for you code, should have been 2-by-2 but yours was 2-by-1