Eigenvalues calculations in Matlab? - 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.

Related

Incorrect value range shown after numeric integration

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

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!!! :)

Matrix dimensions must agree, Newton method

I need to find roots of a function using Newton method. I enter interval and accuracy from keyboard. Here is my code
disp('Newton method')
fx=#(g) 5*sin(g.^3-2*g.^2-1);
fx1=#(g) 5*g*(3*g-4)*cos(-g.^3+2*g.^2+1);
fx2=inline('-5*((4-6*g)*cos(-g.^3+2*g.^2+1)-(4*g-3*g.^2).^2*sin(-g.^3+2*g.^2+1))');
e=input ('Enter accuracy:');
a=input ('enter a:');
b=input ('enter b:');
x0=a:e:b;
y= 5*sin(x0.^3-2*x0.^2-1);
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
plot (x0,y),grid
xlabel('x'),ylabel('y')
fa=fx(a);
n=0;
if (fa*y2>0)
x1=a;
else
x1=b;
end;
while(abs(fx(x1))>e)
n=n+1;
x1=x1-(fx(x1))/(fx1(x1));
end;
disp(sprintf('Answer:%g',x1))
disp(sprintf('Number of iterations:%g',n))
When I compile, it says:
Error using *
Inner matrix dimensions must agree.
Error in Untitled3 (line 10)
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
You are multiplying two 1xn vectors, that is not possible. This multiplication causes the error:
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
^ ^
| |
Using element-wise multiplication .* might be the solution, but I don't know what you try to implement.

Nested integral within integral2 in matlab

I'm attempting to take the double integral (using integral2) of a function that is defined by an integral.
http://i.imgur.com/gIUsLSw.jpg
Here is what I am currently attempting:
t=linspace(0,1,50);
fun_1= #(v) exp(.071*v)
fun = #(x,y) exp(0.14*0.00607*integral(#(u)fun_1(u),0,x)).*exp(-(x-y).^2).*exp(0.14*0.00607*integral(#(u)fun_1(u),0,x));
for i=2:length(t)
for j=i:length(t)
A(i,j)=integral2(fun,t(i-1),t(i),t(j-1),t(j));
end
end
I'm receiving the error
Error using integral (line 86) A and B must be floating point scalars.
Can anyone provide any information on how to fix this problem.
Here you go:
l=3;
t=linspace(0,1,365);
fun3= #(v) integral(#(v)exp(.071*v),0,v,'ArrayValued',true);
for i=2:length(t)
for j=i:length(t)
xx=t(i);
yy=t(j);
fun = #(x,y) exp(0.14*0.00607*fun3(yy)).*exp(-(x-y).^2/l).*exp(0.14*0.00607*fun3(xx));
y(i,j)=integral2(fun,t(i-1),t(i),t(j-1),t(j));
end
end
It works, but it is very slow.

How to solve symbolic equation in matlab

I have an equation in Matlab according to X parameter . I want to find the amount of X for the random amounts of F(x) .
and I tried the code below . but It gives me two different results while my equation should have just one result .
even I tried the roots(f) instead of solve(f) but it gave me an error :
??? Undefined function or method 'isfinite' for input arguments of
type 'sym'.
anybody can help me in this ?
what should I do ?
even if I have a wrong idea about solving this problem please tell me .
Thank you
function betaDistribution_2(a,b)
syms x ;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
% I have this equation and I want to find the amount of x for the random
%amounts of p
p=int(g,x,0,x);
for i=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f);
result=eval(xi);
disp(result)
end
end
Try to filter your solutions.
a=1;
b=2;
% a threshold for imagery part
SMALL=1e-9;
syms x real;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
p=int(g,x,0,x);
% return true for physically meaningfull results
filter = #(xc) abs(imag(xc))<SMALL && real(xc)>0 && subs(g, x, xc) > 0 && subs(p, x, xc)>0;
for m=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f, x);
result=eval(xi);
idx = arrayfun (filter, result);
result = result(idx);
% make sure it is OK
assert(length(result)==1);
disp(result)
end