Question: MATLAB % Step 1 : Create a matrix psiold with vertical values spanning from 0 to 1 6 for every column rows = 1 7

MATLAB
% Step 1: Create a matrix psiold with vertical values spanning from 0 to 16 for every column
rows =17; % Number of rows
cols =30; % Number of columns
psiold = repmat((0:16)',1, cols);
% Step 2: Create a zeros matrix psinew that is the same size as psiold
psinew = zeros(rows, cols);
% Step 3: Create a tolerance variable tol
tol =0.001;
% Step 4: Create a counter variable
counter =0;
% Step 5: While loop condition
while any(any(abs(psinew - psiold)> tol))
% Step 6: Nested for loop to update psinew excluding the boundaries
for i =2:rows-1
for j =2:cols-1
psinew(i,j)=(psiold(i+1,j)+ psiold(i-1,j)+ psiold(i,j+1)+ psiold(i,j-1))/4;
end
end
% Step 7: Define the first column of psinew as values spanning from 0 to 16
psinew(:,1)=(0:16)';
% Step 8: Define the last column of psinew as the same as the second to last column
psinew(:,cols)= psinew(:,cols-1);
% Step 9: Define the first row of psinew as equal to 0
psinew(1,:)=0;
% Step 10: Define the last row of psinew as equal to 16
psinew(rows,:)=16;
% Step 11: Define rows 1 through 9 and columns 21 through 29 of psinew as equal to zero
psinew(1:10,21:cols-1)=0;
% Step 12: If statement to check tolerance and update psiold and psinew
if any(any(abs(psinew - psiold)> tol))
psiold = psinew;
psinew = zeros(rows, cols);
end
% Step 13: Increment the counter
counter = counter +1;
end
% Step 14: Display the counter
disp(['Number of iterations: ', num2str(counter)])
% Step 15: Create a contour plot of psinew
contour(psinew,16)
% Step 16: Set axis equal, label, and title the contour plot
axis equal
xlabel('Column Index')
ylabel('Row Index')
title('Contour Plot of psinew')
% Step 17: Discussion of tolerance changes
% As the tolerance is increased, the counter value (number of iterations) decreases
% because the stopping criterion is met more quickly. Conversely, as the tolerance
% is decreased, the counter value increases because it takes longer to satisfy
% the stopping condition with a stricter tolerance.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!