Incorrect value range shown after numeric integration - matlab

I'm trying to plot the probability of error for the following equation using MATLAB, I want to use the command trapz for the numerical integration, the problem is that I get a fine shape for the plot, but the values in the y-axis are wrong, the whole curve should be between 0 and 1.2 but it is between 0.492 and 0.5!! Can anyone just tell me what is wrong in my code, or just give me a hint? I really need help. Here is my formula that I need to plot (written using Maketex):
This is my code:
close all; clear;clc;
Nr=2;Ns=2;
lmda1=.3; lmda2=.3;
lmdas=.1; lmdar=.1;
z= 0.0001:1:40;
k1=2;k2=2;
kr=2.*Nr;ks=2.*Ns;
ax=0;
avg=0.0001:1:40;
em=1;
ch=2;
for alp=1-k1.*.5:ch
for beta=1-k2.*.5:ch
for eta=0:ch
for N=0:ch
for M=0:ch
for Q=0:ch
for id=0:eta
for jd=0:N
for A=0:N-jd
%
up=.25.*exp(-lmda1./2).*(lmda1./2).^(alp).*(lmda2.^2./(4)).^(beta./2).*exp(-lmda2./2).*(lmda1./(4.*em.*avg)).^eta.*(lmda2./(4.*em.*avg)).^N.*exp(-lmdas.*Ns.*.5).*.25.^(ks.*.25-.5).*exp(-lmdar.*Nr.*.5).*.25.^(kr.*.25-.5).*(Ns.*lmdas.*.25).^M.*(Nr.*lmdar.*.25).^Q;
cy=up.*(1./(factorial(eta).*factorial(N).*factorial(M).*factorial(Q).*gamma(eta+alp+1).*gamma(N+beta+1).*gamma(M+ks.*.5).*gamma(Q+kr.*.5)));
cj=cy.*(factorial(eta)./(factorial(id).*factorial(eta-id))).*(factorial(N)./(factorial(jd).*factorial(N-jd))).*gamma(M+id+jd+ks.*.5);
f1=(cj.*(factorial(N-jd)./(factorial(A).*factorial(N-jd-A))).*em.^A.*(((em+1).^(N-jd-A))).*gamma(kr.*.5+Q+A));
f2=f1.*(2.^(kr.*.5+Q+A)).*avg.^(eta+N);
ax=ax+f2;
end
end
end
end
end
end
end
end
end
q2=2;n2=2;N2=1;eta2=1;
fun2 = exp(-z.*avg.*(1+1.5./avg)).*z.^(eta2+N2-1./2).*(1./((1+z).^(q2).*(1./2+z).^(n2)));
out= trapz(z,fun2);
b=.5.*(1-ax.*(1./sqrt(pi)).*out.*avg.^(1./2));
plot(avg,b);grid;

There was a few wrong expressions in your code. Also I suspect you should evaluate the integral within the loop. What is more your integral meshgrid z seems too coarse. The following code gives me a 0~1.2 range for the second term in P(e)
% close all; clear;clc;
Nr=2;Ns=2;
lmda1=.3; lmda2=.3;
lmdas=.1; lmdar=.1;
z= .01:.01:40;
k1=2;k2=2;
kr=2.*Nr;ks=2.*Ns;
ax=0;
avg=z;
em=1;
ch=2;
for alp=1-k1*.5:ch
for beta=1-k2*.5:ch
for eta=0:ch
for N=0:ch
for M=0:ch
for Q=0:ch
for id=0:eta
for jd=0:N
for A=0:N-jd
%
up=.25.*exp(-lmda1./2).*(lmda1.^2./4).^(alp/2).*(lmda2.^2./(4)).^(beta./2).*exp(-lmda2./2).*(lmda1./(4.*em.*avg)).^eta.*(lmda2./(4.*em.*avg)).^N.*exp(-lmdas.*Ns.*.5).*.25.^(ks.*.25-.5).*exp(-lmdar.*Nr.*.5).*.25.^(kr.*.25-.5).*(Ns.*lmdas.*.25).^M.*(Nr.*lmdar.*.25).^Q;
cy=up./((factorial(eta).*factorial(N).*factorial(M).*factorial(Q).*gamma(eta+alp+1).*gamma(N+beta+1).*gamma(M+ks.*.5).*gamma(Q+kr.*.5)));
cj=cy.*(factorial(eta)./(factorial(id).*factorial(eta-id))).*(factorial(N)./(factorial(jd).*factorial(N-jd))).*gamma(M+id+jd+ks.*.5);
f1=(cj.*(factorial(N-jd)./(factorial(A).*factorial(N-jd-A))).*em.^A.*(((em+1).^(N-jd-A))).*gamma(kr.*.5+Q+A));
C=f1.*(2.^(kr.*.5+Q+A)).*avg.^(eta+N);
q2=Q;
n2=M+id+jd+ks/2;
N2=N;
eta2=eta;
fun2 = exp(-z.*avg.*(1+1.5./avg)).*z.^(eta2+N2-1./2).*(1./((1+z).^(q2).*(1./2+z).^(n2)));
itgrl= trapz(fun2)*.01*.01;
v = avg.^(eta2+N2+1./2);
ax=ax+v.*C.*itgrl;
end
end
end
end
end
end
end
end
end
b=.5-.5/pi^.5 *ax;
plot(avg,b);grid;
I don't know why I need to multiply dz twice but this gives me the correct value range. But I think it has something to do with the v vector values.
>> [min(.5/pi^.5 *ax),max(.5/pi^.5 *ax)]
ans =
0.0002 1.2241

Related

Matlab: Creating a while loop which stops a Taylor Series approximation after it reaches a specified error?

Say I take the taylor series of e^x, for n terms, centered at a=0 for some random value until the error reaches a specified value.
The error is error=(exp(x)-approx)/(exp(x)) and the specified value the error should reach is err=0.01
I have managed to generate a taylor series
x=2;
j=1;
for s=[0:1:10]
approx=approx+(x^s)/(factorial(s))
end
However, I am unable to integrate a while loop which extends the series until the statement error>err is false.
I tried
x=2;
j=1;
err=0.01
error=(exp(x)-approx)/(exp(x));
while error>err
for s=[0:1:10]
approx=approx+(x^s)/(factorial(s))
end
end
and
x=2;
j=1;
err=0.01;
s=-1
error=(exp(x)-approx)/(exp(x));
while error>err
s=s+1
approx=approx+(x^s)/(factorial(s))
end
None of them give the correct answer.
How can I solve the problem.
Make sure your error calculation is within the for loop and initialize the terms:
x=2;
j=1;
err=0.01;
s=0;
error = 1e10;
approx = 0;
while error>err
approx=approx+(x^s)/(factorial(s));
error=(exp(x)-approx)/(exp(x));
s=s+1
end

Eigenvalues calculations in Matlab?

I am going calculate the eigenvalues follow program:
I want to calculate the matrix H , and achieve eigenvalues.
acc=1.44e-10;
a=1.732*acc;
t=-2.550;
x1=0;
y1=0;
z1=0;
x2=acc*cos(60);
y2=acc*sin(60);
z2=0;
sh=0;
for i=-1:1
for j=-1:1
for k=1:2
sh=sh+1;
xk=acc*cos(60)*(k-1);
yk=acc*sin(60)*(k-1);
zk=0;
xx(sh)=(i*a)+(j*a/2)+xk;
yy(sh)=(sqrt(3)/2)*a*j+yk;
zz(sh)=zk;
ki(sh)=k;
R1(sh)=i;
R2(sh)=j;
end
end
end
L0=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2);
r1=0:0.02:1;
kx=(2*pi/(sqrt(3)*a))*(1-r1);
ky=(2*pi/(3*a))*(1-r1);
for ik=1:50
for i=1:2
for j=1:sh
Dis(i+8,j)=sqrt((xx(i)-xx(j))^2+(yy(i)-yy(j))^2+(zz(i)-zz(j))^2);
if abs(Dis-L0)<0.1
Rx=R1(sh)*a+R2(sh)*a/2;
Ry=R2(sh)*sqrt(3)/2*a;
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
end
end
end
end
But I always get this error:
Error in (line 44)
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
Undefined function 'H' for input arguments of type 'double'.
How to solve this error?
If you initialize H in the beginning, the code works.
H = zeros(2,2);
Notice:
I do not know the way you are calculating the eigenvalues. You should use the predefined function eig to check your solution. Another ways is to use SVD to calculate the eigenvectors and eigenvalues.

Matlab: Help Solving a 2nd order ODE with the derivative input being a function of time

I have been going round and round in circles trying to get Matlab to solve the resonator circuit equation with a time varying input voltage. It works just fine as long as all the in arguments of the derivative functions are scalar values.
I have gone through others questions and found answers suggesting anonymous functions or using interpolation. When I try to implement these suggestions, I get a return error that tells me I do not have enough initial conditions to match the output of ode function or the matrices being concatenated do not match..
Here is my code:
%% Define Parameters
f0=1494.72e6;
Q=80;
R=1;
L=(Q*R)/(2*pi*f0);
C=1/((2*pi*f0)^2*L);
tp=71e-9;
N=2^16;
n=(0:N-1);
TT=1e-6;
h=TT/N;
t=n*h;
start_pulse=1;
end_pulse=round(tp/h);
V=zeros(size(t));
%% Create Voltage Pulse
for ii=1:length(n);
if ii>=start_pulse && ii<=end_pulse
V(ii)=sin(2*pi*f0*t(ii));
end
end
%%
tspan=[0 TT];
x0=[0 0];
sol=ode45(#ode,tspan,x0,[],V);
int=(0:h:TT);
sint=deval(sol,int);
plot(int,sint*C);
MY ode funtion is the following:
function [ dx ] = ode( t,x,V)
f0=1494.72e6;
Q=80;
R=1;
L=(Q*R)/(2*pi*f0);
C=1/((2*pi*f0)^2*L);
dx1 = x(2);
dx2 =((-x(1)./(L*C))-(R*x(2)./L)-(V./(L*C)));
dx = [dx1; dx2];
end
As you can see, L,C,and R all are scalar values. If I replace 'V' in dx2 with '1', the program runs just fine. I need to change V to be the matrix defined above.
Any help at all would be greatly appreciated!!! Thanks in advance!!! :)

Matlab Newton Raphson

I asked for help about Matlab task few weeks ago but removed it since it took me some time to solve. Unfortunately, I still have a problem.
Task is: Find a real root of the function f(x)=tanh(x^2 - 9) using at least 3 iterations, using Newton-Raphson method. x= 3.2 show each iteration graphically.
In code "pog" means min. mistake, "br" is counter.
If I don't put a counter, it immediately gives me "NaN", with counter it does write first few calculations.
My code:
clc
clear all
x=3.2;
fx=tanh(x^2-9);
iter=5;
pog=0.01;
br=1;
while br<10;
xk= x-((tanh(x^2-9))/(-2*x*(tanh(x^2 - 9)^2 - 1)));
fprintf ('x=%g\txk=%g\t%g\n', x,xk, abs(xk-x))
if pog>abs(xk-x);
break
end
x=xk;
br=br+1;
end
Thank you in advance!
As far as showing the iterations graphically, this is the best I can do:
clc
clear all
close
G=zeros(20,10);
X=linspace(2.5,3.5,20)
G(:,1)=X;
for i=1:length(X)
x=X(i)
fx=tanh(x^2-9);
pog=0.0001;
br=1;
while br<10;
xk= x-((tanh(x^2-9))/(2*x*sech(9-x^2)^2));
G(i,br+1)=xk;
x=xk;
br=br+1;
end
end
I=tanh(G(:,end).^2-9)<1e-5;
X(I)
plot(G,1:10)
hold off
axis([2.5 3.5 0 5])
X(I) is a list of starting values the converge to the root, and the plot shows iteration number on the y-axis, and the guess at that iteration on the x-axis. You can follow each starting value through and see what happens.
Here's another way of visualising Newton's method. It shows the tangent line that is constructed, and where it passes through 0 which gives you the new x values, from which a vertical line gives you the new function value, which defines a new tangent line for the next iteration. It might help.
clc
clear all
close
G=zeros(20,10);
X=linspace(2.75,3.25,20)
G(:,1)=X;
x2=2:.01:4;f2=#(x) tanh(x.^2-9); %// to help with plotting
for i=1:length(X)
x=X(i)
fx=tanh(x^2-9);
pog=0.0001;
br=1;
xk=x;
while br<10;
%// Newton method step
dx=((tanh(x^2-9))/(2*x*sech(9-x^2)^2));
xk=x-dx;
%// plotting everything
G(i,br+1)=xk;
plot(x2,f2(x2))
hold all
plot(G(i,br:br+1),f2(G(i,br:br+1)),'.','MarkerSize',16)
plot(x2,f2(x)+(x2-x)./(xk-x).*(-f2(x)))
plot(xk,0,'.','MarkerSize',16)
plot(x2,(x2-xk)*(2*xk*sech(9-xk^2)^2)+f2(xk))
plot([xk xk],[-1 1])
plot([2 4],[0 0],'k')
axis([2 4 -1 1])
drawnow
pause
hold off
%// finishing Newton step
x=xk;
br=br+1;
end
hold off
end

How to plot this function in matlab

The problem is how to plot the below equation in discrete form:
g=(1-exp(-1i*pi*k))/(1-exp(-1i*pi*k/50)) where k ranges from -300 to 300
When I execute the program I always get the following error:
"Attempted to access (-299); index must be a positive integer or logical."
I don't really know what it means, I tried to exclude the even value of pi but still the same problem. I need a hand here please.
try:
clear all
k = -300:300;
g = (1-exp(-1i*pi*k))./(1-exp(-1i*pi*k/50));
subplot(3,1,1)
plot(k,real(g))
ylabel('real(g)')
xlabel('k')
subplot(3,1,2)
plot(k,imag(g))
ylabel('imag(g)')
xlabel('k')
subplot(3,1,3)
plot(g)
xlabel('real(g)')
ylabel('imag(g)')
I'm affraid you could have overriden exp and this caused a problem