I cant figure out where i went wrong with my code

Hello i have some code for a class. i will include a picture of what my instructor is asking for us to do so yall understand what i am attempting. when solving teh temperature of the oil in the matrix so i can plot it later i am getting wierd values for the oil and then it stops computing this should not happen as long as the values alll approach the same value but they seem to be very different all over even with my bounds all set. any help would be greatly appreciated. my code is also included below.
N=65;
l=N-1;
k=N-1;
A=125;
B=50;
r=linspace(0,1,N);
theta=linspace(-pi/2,pi/2,N);
delr=1/k;
dell=1/l;
M=10000;
Ts=A+B*sin(theta);
epsilon=10^-5;
iter=1;
T = zeros(N,N);
f=zeros(N,N);
error = zeros(1,M);
delerr = zeros(1,M);
etime = zeros(1,M);
etime(1) = 0;
tic;
T(:,1)=125;
Tct=linspace(125,175,N);
Tcb=linspace (125,75,N);
T;
while (iter <= N)
iter=iter+1;
for j=1:N
T(j,N)=Ts(j);
end
end
iter=1;
T;
while (iter <= N)
iter=iter+1;
for i=1:N
T(1,i)=Tct(i);
T(N,i)=Tcb(i);
end
end
T;
iterations=1;
error(1) = norm(T);
delerr(1) = error(1);
while (iterations+1 <= M) && (delerr(iterations) > epsilon)
iterations = iterations+1;
for j = 2:k
for i = 2:l
%T(i,j) = (1/4)*(T(i-1,j)+T(i+1,j)+T(i,j-1)+T(i,j+1)-f(i,j));
T(i,j)= (1/2)*((delr^2+dell^2*r(i)^2)/dell^2*delr^2*r(i)^2)*(((T(i+1,j)+T(i-1,j))/(delr^2)))+((1/r(i))*((T(i+1,j)-T(i-1,j))/(2*delr)))+(1/r(i)^2)*((T(i,j+1)+T(i,j-1))/(dell^2));
end
end
error(iterations) = sqrt(sum(sum((T).^2)));
delerr(iterations)=abs(error(iterations)-error(iterations-1));
etime(iterations)=toc;
end
figure(1)
semilogy(delerr);
title('Delta Error vs Iterations','fontsize',14);
xlabel('Iterations','fontsize',14);
ylabel('\Delta \epsilon (L_2 Norm of Error)','fontsize',14);
set(gca,'Ygrid','on');
figure(2)
semilogy(etime,delerr);
title('Error Vs Time','fontsize',14);
xlabel('Time (sec)','fontsize',14);
ylabel('\Delta \epsilon (L_2 Norm of Error)','fontsize',14);
set(gca,'Ygrid','on');
figure(3)
contourf(r,theta,T,14);
title('Contour plots','fontsize',14);
xlabel('r','fontsize',14);
ylabel('theta','fontsize',14);
colorbar;
T;

댓글 수: 2

Rik
Rik 2021년 8월 15일
Your code lacks comments, so it is difficult to understand. Your text also lacks a grammar check, making it seem as if you didn't put in any effort (which doesn't encourage others to put in effort).
It is also unclear where exactly the problem occurs and what behavior is the unwanted behavior. Have you tried using the debugger to execute your code line by line?
Well i can tell you that after at least 12 hours working on this code yesterday to get it right i figured out why when doing numbers of N greater than 200 i was getting odd results using the cartesian version of laplace equation. it was becuase my iterations were off. I didn't debug as i did not know that a debug tool existed. I can tell you that as soon as I switch from the cartesian version of laplace equation to the polar version my calculation for the matrix is completely jacked up. I cant figure out as to why that is however i do know that re typing the code did not help to make sure i didnt mess up that portion of the equation but it basically attempts to calculate some instance of the temp matrix and then the numbers are either so small or large that it just stops and does not fill in the remaining portions of the matrix giving them NaN value.
currently this is the code i am using but still hvaing the problem with the Temp matrix not calculating properly when attmepting to use the polar version.
clear
close all
clc
N=65;
l=N-1;
k=N-1;
A=125;
B=50;
r=linspace(0,1,N);
theta=linspace(-pi/2,pi/2,N);
delr=1/k;
dell=(1/l)*pi;
M=100000;
Ts=A+B*sin(theta);
epsilon=10^-6;
iter=1;
T = zeros(N,N);
f=zeros(N,N);
error = zeros(1,M);
delerr = zeros(1,M);
etime = zeros(1,M);
etime(1) = 0;
tic;
T(1,:)=125;
Tct=linspace(125,175,N);
Tcb=linspace (125,75,N);
T;
while (iter <= N)
iter=iter+1;
for j=1:N
T(N,j)=Ts(j);
end
end
iter=1;
T;
while (iter <= N)
iter=iter+1;
for i=1:N
T(i,N)=Tct(i);
T(i,1)=Tcb(i);
end
end
%x;
%y
%T
%r
%dell
iterations=1;
error(1) = norm(T);
delerr(1) = error(1);
while (iterations+1 <= M) && (delerr(iterations) > epsilon)
iterations = iterations+1;
for j = 2:l
for i = 2:k
T(i,j) = (1/4)*(T(i-1,j)+T(i+1,j)+T(i,j-1)+T(i,j+1)-f(i,j));
%T(j,i) =(1/(2*((1/(dell^2*r(j)^2))+((1/(delr^2))))))* (T(j-1,i)+T(j+1,i))/(delr^2)+1/r(j)*(T(j+1,i)-T(j-1,i))/(2*delr)+1/(r(j)^2)*(T(j,i-1)+T(j,i+1))/(dell^2);
end
end
error(iterations) = sqrt(sum(sum((T).^2)));
delerr(iterations)=abs(error(iterations)-error(iterations-1));
etime(iterations)=toc;
end
iterations
x = zeros(N,N);
y = zeros(N,N);
for j = 1:N
for k = 1:N
x(j,k) = r(j)*cos(theta(k));
y(j,k) = r(j)*sin(theta(k));
end
end
%T
figure(1)
semilogy(delerr);
title('Delta Error vs Iterations','fontsize',14);
xlabel('Iterations','fontsize',14);
ylabel('\Delta \epsilon (L_2 Norm of Error)','fontsize',14);
set(gca,'Ygrid','on');
figure(2)
semilogy(etime,delerr);
title('Error Vs Time','fontsize',14);
xlabel('Time (sec)','fontsize',14);
ylabel('\Delta \epsilon (L_2 Norm of Error)','fontsize',14);
set(gca,'Ygrid','on');
figure(3)
contourf(x,y,T,14);
title('Contour plots','fontsize',14);
%xlabel('theta(-pi/2)','fontsize',14);
%ylabel('r','fontsize',14);
colorbar;

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 8월 14일

편집:

2021년 8월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by