My instructor said you only need to change few things from bottom code to solve
ID: 3006648 • Letter: M
Question
My instructor said you only need to change few things from bottom code to solve this problem
%------------------------------------------------
% Solve the ODE BV problem f_xx + f_yy = 0
% with boundary conditions: f = 1, f = 2,
% f = 3, and f = 4
% using iterative finite difference approach.
%
% Note: we are using i and j here...not cool in
% general.
%------------------------------------------------
clear all
close all
clc
nFrames = 200; % number of frames
[x,y] = meshgrid(0 : 0.04 : 1); % make x-y grid
f = 4*ones(length(x), length(y)); % initialize f values
f(:,1) = 1;
f(:,length(x)) =3; % ...and the boundaries
f(1,:) =2;
f(length(y),:) =4;
for i = 1 : nFrames % for each plot...
for j = 2 : (length(y)-1)
for k = 2 : (length(x)-1)
f(k,j) = (f(k-1,j)+f(k+1,j)+f(k,j-1)+f(k,j+1))/4;
end
end
mesh(x,y,f),... % make one plot
xlabel('X axis'),...
ylabel('Y axis'),...
zlabel('F axis'),...
title('The plot title'),...
axis([0 1 0 1 0 4]);
% axis off
shading interp
drawnow
end
beep % hey, I'm done
Please give me the code that works
Explanation / Answer
For checking convergence the solution is stored in two arrays. One is the array f which stores the current iteration solution and another is the fprev which stores solution in previous iteration
Now we calculate maximum value in: abs(fprev-f) and choose that to be the error,e
This makes sense because if the max (abs(fprev-f)) falls below some tolerance then all values of f will will within that : fprev +- tolerance.
And code runs till e falls below some specified tolerance. In code below we have specified:
tolerance = 1e-8
The modified code with convergence criterion is:
%------------------------------------------------
% Solve the ODE BV problem f_xx + f_yy = 0
% with boundary conditions: f = 1, f = 2,
% f = 3, and f = 4
% using iterative finite difference approach.
%
% Note: we are using i and j here...not cool in
% general.
%------------------------------------------------
clear all
close all
clc
nFrames = 200; % number of frames
[x,y] = meshgrid(0 : 0.04 : 1); % make x-y grid
f = 4*ones(length(x), length(y)); % initialize f values
f(:,1) = 1;
f(:,length(x)) =3; % ...and the boundaries
f(1,:) =2;
f(length(y),:) =4;
fprev = f;
err = 1;
tolerance=1e-8;
h = 0.1;
while err > tolerance
for j = 2 : (length(y)-1)
for k = 2 : (length(x)-1)
f(k,j) = (f(k-1,j)+f(k+1,j)+f(k,j-1)+f(k,j+1)-(h^2)*(x(k,j)+y(k,j)))/4;
err = max(max(abs(fprev-f)));
fprev = f;
end
end
mesh(x,y,f),... % make one plot
xlabel('X axis'),...
ylabel('Y axis'),...
zlabel('F axis'),...
title('The plot title'),...
axis([0 1 0 1 0 4]);
% axis off
shading interp
drawnow
end
beep % hey, I'm done
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.