Confused about the specs of the project - matlab

Im confused about what the project is saying. It says the following:
I did the #1 but in #2 where do I have to plug Aij = cos(...) in inside the code?
I have the code but have no clue where I should plug the given entry Aij in. Any help would be appreciated! Thanks!
Code
n=8;
for i=1:n
for j=1:n
a(i,j)=1/(i+j-1);
end
end
c=ones(n,1);
b=a*c;
aorig=a;borig=b;
%Elimination
for i=1:n-1
for j=i+1:n
m=a(j,i)/a(i,i);
for k=i:n
a(j,k)=a(j,k)-m*a(i,k);
end
b(j)=b(j)-m*b(i);
end
end
%Back substitution
for i=n:-1:1
for j=i+1:n
b(i)=b(i)-a(i,j)*x(j);
end
x(i)=b(i)/a(i,i);
end
x'

Replace your Hilbert matrix definition a(i,j)=1/(i+j-1) with the cosine expression from #2.

Related

Coupled Poisson equations and Matlab program

can anyone help me solve it? Or help me uncouple the equations? :(
$u_{xx}+u_{yy}=au+bw$
$w_{xx}+w_{yy}=cu+dw$
Also I have to solve this problem using the finite difference method, but once i get the output everything's zero. This is my Matlab code, if anyone could help me with that.
this is my code so far
clear; close all;
n=101; h=1/(n-1);
x=0:h:1.01; y=0:h:1.01;
U=zeros(n+1); W=zeros(n+1);
omega=1.7777777;err=1000;
a1=0.5; a2=5; b1=-1; b2=1;
tol=1e-6;
for i=1:n+1 %boundary conditions
U(1,i)=cos(5*pi*y(i)); U(i,1)=cos(5*pi*y(i)); U(n+1,i)=0; U(i,n+1)=0;
W(1,i)=cos(5*pi*y(i)); W(i,1)=cos(5*pi*y(i)); W(n+1,i)=0; W(i,n+1)=0;
end
figure(1);
subplot(1,2,1);mesh(x,y,U);view(130,25);grid on; title('boundary conditions U(x,y)');
subplot(1,2,2);mesh(x,y,W);view(130,15);grid on; title('boundary conditions W(x,y)');
u=U; w=W;
while err>tol
for i=2:n
for j=2:n
u(i,j)=(1-omega)*U(i,j)+omega*(((u(i+1,j)+u(i-1,j)+u(i,j+1)+u(i,j-1))/h^2)-b1*W(i,j))/(a1+(4/h^2));
w(i,j)=(1-omega)*W(i,j)+omega*(((w(i+1,j)+w(i-1,j)+w(i,j+1)+w(i,j-1))/h^2)-a2*U(i,j))/(b2+(4/h^2));
end
end
W=w; U=u; err=max(max(abs(u-U)));
end
figure(2);
subplot(1,2,1);mesh(x,y,U);view(130,25);grid on; title('U(x,y)');
subplot(1,2,2);mesh(x,y,W);view(130,15);grid on; title('W(x,y)');
The problem is this line:
W=w; U=u; err=max(max(abs(u-U)));
You are setting U=u`` first and then looking at the different of uandU```. Reverse the computation and it works.
err=max(max(abs(u-U))); W=w; U=u;

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

Error in using fzero

I am trying to solve a non-linear equation by using a while loop in Matlab but it's not giving me the results and give me an error in line that uses fzero function.
Why does this happens? How to solve the issue? Could someone help me with this?
Thanks in advance.
function z=optp(z,p)
z=3.067;
p=0;
while (z-p) > 0.8
syms a y Q p
p=z;
w=3;
C=1;
P=5;
b=1.2;
c=8;
B=0.5;
R=A^B*c*p^-b;
Q=(0.5-1/(p+5))*2400*((A^0.5)/(p^(6/5)));
x=Q;
J1=int(int(a*x*0.01,y,a*x/R,100),a,0,Q/x);
J2=(b*x^2*0.01/R)*int(a^2*1,a,0,Q/x);
J3=(P*R*b/p)*(int(int(y*0.01,y,a*x/R,100),a,0,Q/x));
J4=(b*R^2/x)*int(y^2*0.01,y,0,Q/R);
J5=(R*0.2)*(int(int(y*0.01,a,y*R/x,1),y,0,Q/R));
J=J1+J3+J4-J2-J5;
z=fzero(# (p) J,p)
end
end

Gauss-Seidel method doesn't work for large sparse arrays?

Once again I have a problem with the Gauss-Seidel Method in Matlab. Here it is:
function [x] = ex1_3(A,b)
format long
sizeA=size(A,1);
x=zeros(sizeA,1);
%Just a check for the conditions of the Gauss-Seidel Method (if it has dominant diagonal)
for i=1:sizeA
sum=0;
for j=1:sizeA
if i~=j
sum=sum+abs(A(i,j));
end
end
if abs(A(i,i))<sum
fprintf('\nGauss-Seidel''s conditions not met!\n');
return
end
end
%Actual Gauss-Seidel Method
max_temp=10^(-6); %Pass first iteration
while max_temp>(0.5*10^(-6))
xprevious=x;
for i=1:sizeA
x(i,1)=b(i,1);
for j=1:sizeA
if i~=j
x(i,1)=x(i,1)-A(i,j)*x(j,1);
end
end
x(i,1)=x(i,1)/A(i,i);
end
x
%Calculating infinite norm of vector x-xprevious
temp=x-xprevious;
max_temp=temp(1,1);
for i=2:sizeA
if abs(temp(i,1))>max_temp
max_temp=abs(temp(i,1));
end
end
end
It actually works fine for a 100x100 matrix or smaller. However, my tutor wants it to work for 100000x100000 matrices. At first it was difficult to even create the matrix itself, but I managed to do it with a little help from here:
Matlab Help Center
Now, I call the ex1_3 function with A as a parameter, but it goes really slow. Actually it never ends. How can I make it work?
Here's my code for creating the specific matrix my tutor wanted:
The important part is just that it meets these conditions:
A(i; i) = 3, A(i - 1; i) = A(i; i + 1) = -1 n=100000
b=ones(100000,1);
b(1,1)=2;
b(100000,1)=2;
i=zeros(299998,1); %Matrix with the lines that we want to put nonzero elements
j=zeros(299998,1); %Matrix with the columns that we want to put nonzero elements
s=zeros(299998,1); %Matrix with the nonzero elements.
number=1;
previousNumberJ=0;
numberJ=0;
for k=1:299998 %Our index in i and j matrices
if mod((k-1),3)==0
s(k,1)=3;
else
s(k,1)=-1;
end
if k==1 || k==2
i(k,1)=1;
j(k,1)=k;
elseif k==299997 || k==299998
i(k,1)=100000;
j(k,1)=(k-200000)+2;
else
if mod(k,3)==0
number=number+1;
numberJ=previousNumberJ+1;
previousNumberJ=numberJ;
end
i(k,1)=number;
j(k,1)=numberJ;
numberJ=numberJ+1;
end
end
A=sparse(i,j,s); %Creating the sparse array
x=ex1_3(A,b);
the for loop works very slowly in Matlab, perhaps you may want to try the matrix form of the iteration:
function x=gseidel(A,b)
max_temp=10^(-6); %Pass first iteration
x=b;
Q=tril(A);
r=b-A*x;
for i=1:100
dx=Q\r;
x=x+1*dx;
r=b-A*x;
% convergence check
if all(abs(r)<max_temp) && all(abs(dx)<max_temp), return; end
end
For your A and b, it only takes 16 steps to converge.
tril extracts the lower triangular part of A, you can also obtain this Q when you build up the matrix. Since Q is already the triangular matrix, you can solve the equation Q*dx=r very easily if you are not allowed to use \ function.

Matlab vectorization

I've been trying to vectorize this function for matlab:
function [Q,R]=gramSchmidtMod(A)
n=size(A,1);
R=zeros(n);
for j=1:n
R(j,j)=norm(A(:,j));
Q(:,j)=A(:,j)/R(j,j);
for i=j+1:n
R(j,i)=Q(:,j)'*A(:,i);
A(:,i)=A(:,i)-Q(:,j)*R(j,i);
end
end
end
i tried:
j=1:n
R(j,j)=norm(A(:,j));
Q(:,j)=A(:,j)/R(j,j);
i=j+1:n
R(j,i)=Q(:,j)'*A(:,i);
A(:,i)=A(:,i)-Q(:,j)*R(j,i);
but that doesn't respect the same order as it would when using two for loops.
Can anyone help me out here ?
Why won't you just use
[Q,R]=qr(A)