Coupled Poisson equations and Matlab program - matlab

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;

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

Confused about the specs of the project

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.

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

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)