Question
!!!!!!!!!!!!!!!!!!!!!!!!! First of all, this is a Matlab Question, and it is very important to me. Do not worry, I almost done 70% part of
!!!!!!!!!!!!!!!!!!!!!!!!!
First of all, this is a Matlab Question, and it is very important to me.
Do not worry, I almost done 70% part of these problem , and i am only worry about part D,E and F.
this is my code
% we want to write as [A][Y]=[B] % Here, Y is unknows function [Yhistory,ErrorX,ErrorR,Iter] = Jacobi(L,a,YL,YR,M,tolX,tolR)
M= 201; % Number of points along cable to calculate Y(x) L= 10; % Distance between supports (m) a= 2; % Physical characteristic of cable (see problem) YL= 20; % Height of cable at Left suport, (m) YR= 15; % Height of cable at Right support, (m) tolX= 1e-8 * sqrt(M); % Convergence tolerance for "proxy error in X" tolR= 1e-8 * sqrt(M); % Convergence tolerance for residual error Delx= L/(M-1);
% For this part, try to get matrix[A] A(1,1)=1; A(M,M)=1;
for i= 2:M-1 A(i,i+1)=((1/(Delx)^2)+(1/(4*a*Delx))); A(i,i)=(-2/(Delx)^2); A(i,i-1)=((1/(Delx)^2)-(1/(4*a*Delx))); end
% For this part, we try to get Matrix [B] for i=2:M-1; b(i)=1/a; end b(1)=YL; b(M)=YR; B=b';
% For this part, we try to get our unknows [Y] D=zeros(size(A)); Dinv=zeros(size(A));
for i=1:M; D(i,i)=A(i,i); Dinv(i,i)=(1/A(i,i)); end offD= A-D; % This is our perdiction of first unknows [Y],we call this Y(0) already given. % 0 means that number of interations Yold=ones(M,1)*mean([YL YR]); % This is our second unknow [Y], we call that Y[1],and we get it by % submite Y[0] into Yknow equation Ynew= Dinv*(B-offD*Yold);
% we try to find the error here by using norm %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! % Could you help me here, I have no ideas for this while loop %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! X= norm(Ynew-Yold,2); R= norm(A*Ynew-b,2); k=0 while (X > tolX) || (R > tolR) k=k+1; Yorder=Ynew;; if k==1 Yhistory=Yold; ErrorX= NaN; ErrorR= norm(A*Yold-b,2); if k>1 Ynew= Dinv*(B-offD*Yold); Yhistory(:,k)= Ynew; ErrorX(k)= norm(Ynew-Yold,2); ErrorY(k)= norm(A*Ynew-b,2); end end end
% JACOBI uses the Jacobi method to solve for the height Y(x) for M % equally-spaced points of a sagging cable supported at each end. % % INPUTS % L Distance between cable supports (m) % a Physical characteristic of cable (see problem sheet) % YL, YR Height at left & right ends of cable % M Number of equally-spaced points picked to solve Y(x) % tolX Convergence tolerance for "proxy error" in Y vector (L2 norm) % tolR Convergence tolerance for residual error (L2 norm)
% OUTPUTS % Yhistory Each column of Yhistory will be the height distribution % Y(x) for each Jacobi iteration. Size: (M) x (# Iterations) % ErrorX Vector (length: Iter) with L2-norm of "proxy error in X" for % each iteration. For the initial guess, just set ErrorX= NaN. % ErrorR Vector (length: Iter) with L2-norm of "residual error" for % each iteration. % Iter Total number of iterations it took to converge.
% ** YOU ** need to write and document this entire routine to provide the % required outputs. Key things to remember: % 1. For your initial guess, pick Y= (YR+YL)/2 for all the elements. % 2. Use a "while loop" to keep iterating until you meet BOTH convergence % criteria. % 3. Make sure the first (left-most) column of Yhistory is your initial % guess. Then each subsequent column (to the right) stores the next % iteration of the Y vector. The very last column in Yhistory would % then be your "final" answer.
Your job is just to make the function Jacobi.m that does the following: a) EXACTLY interfaces with my main script, using all the same inputs and outputs. To help, I've actually provided the first line of the function, to ensure compatibility. DON'T CHANGE THIS!! function [Yhistory, Errorx, ErrorR, Iter-Jacobi (L, a, YL, YR, M, tolX, tolR) b) Create the A and b matrices that represent the M equations for Y, in the form A Y= b where Y= [Y, , Yn ]T. Make sure you understand my example for M = 6 on the last page, before tackling this general Mx M system. c) For your initial guess, y, just use the average value between the supports y (Ya%) for all d) Use a "while" loop to keep iterating until the L2-norms of both the "proxy" error between iterations and the residual error are within their tolerances e) Calculate the column vector Yx for each iteration k using the Jacobi iterative method. f) Store each iteration as a column vector in the variable Yhistory. So, for example, if you're done after 1000 iterations, then Yhistory would be a 201 x 1000 matrix, with each column vector Y* representing one iteration. This will allow us to make plots of what the iterations looked like later ) Calculate (and store in vectors Errorx and ErrorR) the L2-norms of both the "proxy" error in Y and the residual error. This will allow us to make a convergence history plot later. Your job is just to make the function Jacobi.m that does the following: a) EXACTLY interfaces with my main script, using all the same inputs and outputs. To help, I've actually provided the first line of the function, to ensure compatibility. DON'T CHANGE THIS!! function [Yhistory, Errorx, ErrorR, Iter-Jacobi (L, a, YL, YR, M, tolX, tolR) b) Create the A and b matrices that represent the M equations for Y, in the form A Y= b where Y= [Y, , Yn ]T. Make sure you understand my example for M = 6 on the last page, before tackling this general Mx M system. c) For your initial guess, y, just use the average value between the supports y (Ya%) for all d) Use a "while" loop to keep iterating until the L2-norms of both the "proxy" error between iterations and the residual error are within their tolerances e) Calculate the column vector Yx for each iteration k using the Jacobi iterative method. f) Store each iteration as a column vector in the variable Yhistory. So, for example, if you're done after 1000 iterations, then Yhistory would be a 201 x 1000 matrix, with each column vector Y* representing one iteration. This will allow us to make plots of what the iterations looked like later ) Calculate (and store in vectors Errorx and ErrorR) the L2-norms of both the "proxy" error in Y and the residual error. This will allow us to make a convergence history plot laterStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started