{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

HW 9 Solution

# HW 9 Solution - if(fL*fM< 0 Left half xU = xM else xL =...

This preview shows pages 1–4. Sign up to view the full content.

HW 9 Solution

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
% Part I. Finite difference A = zeros(11); b = zeros(11,1); % x = 0 BC A(1,1) = 1; b(1) = 2; % x = 10 BC A(11,9) = 1/2; A(11,10) = -4/2; A(11,11) = 3/2; b(11) = 1; % Interior points for i = 2:10 A(i,i-1) = -3; A(i,i) = 11; A(i,i+1) = -3; end cfd = inv(A)*b; xfd = [0:1:10]; % Part II. Shooting xL = -3; xU = -2.5; % fL z0 = [2; xL]; [x,z] = ode45(@myfunhw9,[0 10],z0); fL = z(length(z),2) - 1; % fU z0 = [2; xU]; [x,z] = ode45(@myfunhw9,[0 10],z0); fU = z(length(z),2) - 1; for i = 1:100 xM = (xL+xU)/2;
z0 = [2; xM]; [x,z] = ode45(@myfunhw9,[0 10],z0); fM = z(length(z),2) - 1;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (fL*fM < 0) % Left half xU = xM; else xL = xM; fL = fM; end end xsh = x; csh = z; figure(2) plot(xfd,cfd,'x',xsh,csh) xlabel('x (m)') ylabel('c (mol/m^3)') legend('FD','shooting') % Part II. Plot for shooting guess = [-3:0.01:-2.5]; n = length(guess); er = zeros(n,1); for i = 1:n z0 = [2; guess(i)]; [x,z] = ode45(@myfunhw9,[0 10],z0); er(i) = z(length(z),2) - 1; end figure(1) plot(guess,er) xlabel('guess for z_2(0)') ylabel('error between z_2(10) and dcdx(10)') function dzdx = myfunhw9(x,z) dzdx = zeros(2,1); dzdx(1) = z(2); dzdx(2) = 5/3*z(1);...
View Full Document

{[ snackBarMessage ]}

### Page1 / 4

HW 9 Solution - if(fL*fM< 0 Left half xU = xM else xL =...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online