I cannot format the first couple lines of my MATLAB code correctly and it is obv
ID: 3833270 • Letter: I
Question
I cannot format the first couple lines of my MATLAB code correctly and it is obviously ruinning the rest of the program I wrote. If you could help me reformate the first couple lines so that I actually create the equation correctly that would be great. By the first couple lines I am specifically talking about the 3rd line with the 2nd order differential equation. I guess that is not the correct format for declaring an equation. If you could help me debug the rest of my code that would be AMAZING! thanks in advance
function m371bvp1d
% template for numerical solution of a two-point boundary value problem
-diff(y,2) == 25*sin(pi()*x), y(0)= 0, y(1)= 1 ;
clear; clf;
y(x) = 25/(pi()^2)*sin(x*pi())+x;
for icase=1:9
n = 2^(icase-1); h = 1/(n+1); % h = mesh size
xe = 0:0.0025:1; % fine mesh for plotting exact solution
ye = arrayfun(y,xe); % exact solution on fine mesh
% Set up for numerical solution.
for i=1:n
xh(i) = i*h; % mesh points
yh(i) = y(i); % exact solution at mesh points
a(i) = -1 ; b(i) = 1; c(i) = -1; % matrix elements
r(i) = -(1/h^2)*(yh(n+1)-2*yh(n)+yh(n-1)); % right hand side vector
end
r(1) = r(1)+(1/h^2)*alpha; % adjust for BC at x=0
r(n) = r(n)+(1/h^2)*beta; % adjust for BC at x=1
wh = LU_371(a,b,c,r); % numerical solution
% output
table(icase,1) = h;
table(icase,2) = norm(yh-wh,inf);
table(icase,3) = norm(yh-wh,inf)/h;
table(icase,4) = norm(yh-wh,inf)/h^2;
table(icase,5) = norm(yh-wh,inf)/h^3;
xplot = [0 xh 1]; wplot = [alpha wh beta];
subplot(3,3,icase); plot(xe,ye,xplot,wplot,'-o');
string = sprintf('h=1/%d',n+1); title(string)
end
table
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function w = LU_371(a,b,c,r)
% input: a, b, c, r - matrix elements and right hand side vector
% output: w - solution of linear system
n = length(r);
u(1) = b(1);
for k=2:n
l(k)=a(k)/u(k-1);
u(k)=b(k)-l(k)*c(k-1);
end
z(1) = r(1);
for k=2:n
z(k)= r(k)-l(k)*c(k-1);
end
w(n) = z(n)/u(n)
for k=n-1:-1:1
w(k) = (z(k)-c(k)*w(k+1))/u(k);
end
return w
Explanation / Answer
% template for numerical solution of a two-point boundary value problem
% -y''=r, y(0)=alpha, y(1)=beta
clear; clf;
alpha = 0; beta = 1; % boundary conditions
for icase=1:4
n = 2
^
icase-1; h = 1/(n+1); % h = mesh size
xe = 0:0.0025:1; % fine mesh for plotting exact solution
ye =
:::
; % exact solution on fine mesh
% Set up for numerical solution.
for i=1:n
xh(i) = i*h; % mesh points
yh(i) =
:::
; % exact solution at mesh points
a(i) =
:::
; b(i) =
:::
; c(i) =
:::
; % matrix elements
r(i) =
:::
; % right hand side vector
end
r(1) =
:::
; % adjust for BC at x=0
r(n) =
:::
; % adjust for BC at x=1
wh = LU
371(a,b,c,r); % numerical solution
% output
table(icase,1) = h;
table(icase,2) = norm(yh-wh,inf);
table(icase,3) = norm(yh-wh,inf)/h;
table(icase,4) = norm(yh-wh,inf)/h
^
2;
table(icase,5) = norm(yh-wh,inf)/h
^
3;
xplot = [0 xh 1]; wplot = [alpha wh beta];
subplot(2,2,icase); plot(xe,ye,xplot,wplot,'-o');
string = sprintf('h=1/%d',n+1); title(string)
end
table
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function w = LU
371(a,b,c,r)
% input: a, b, c, r - matrix elements and right hand side vector
% output: w - solution of linear system
n = length(r);
%
% Fill in the steps below using the tridiagonal LU method given in class.
%
% find L, U
%
% solve Lz = r
%
% solve Uw = z
%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.