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
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?
Dylan Bull
2021년 8월 15일
편집: Walter Roberson
2021년 8월 15일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!