Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

XML Data Management Native XML And XML Enabled Database Systems

Authors: Akmal Chaudhri, Awais Rashid, Roberto Zicari, John Fuller

1st Edition

0201844524, 978-0201844528

More Books

Students also viewed these Databases questions

Question

#finish the code import pandas as pd small _ df = . . .

Answered: 1 week ago

Question

Carry out an interview and review its success.

Answered: 1 week ago