You are on page 1of 4

Worksheet 7 Q4. Function has two sums, one of which contains N terms. The other has a specified M terms.

Also contains iterative fprintf command.

Function File Q4f.m


% Q4f.m function [N V P] = Q4f(t,M) Vsum = 0; for n = 1:M Vsum = Vsum + (-1)^(n-1)*4/(2*n-1)/pi*cos((2*n-1)*pi*t/2); end V = 1 + Vsum; Pnew = 0; Pold = 1; k = 0; while Pnew ~= Pold k = k + 1; Pold = Pnew; Pnew = Pnew + (-1)^(k-1)/exp(k/5)*sin(k*t); end P = pi + Pnew; N = k;

Script File Q4s.m


% Q4s.m clc;clear all;close all M = input('Please enter M: '); t_0 = 0; t_f = 25; n = 500; t = linspace(t_0,t_f,n); V = zeros(1,n); P = zeros(1,n); for i = 1:n [~,V(i),~] = Q4f(t(i),M); [~,~,P(i)] = Q4f(t(i),M); fprintf('At time %3.1f, P requires %g terms to converge \n',t(i),Q4f(t(i),M)) end subplot(211),plot(t,V) subplot(212),plot(t,P)

Q5. Function calculates two outputs for one input. The script uses hold on and hold off to plot horizontal and vertical oscillatory lines between two curves.

Function File Q5f.m


% Q5f.m function [y1 y2] = Q5f(x) y1 = x; y2 = 0.5*x.*(4-x).*sin(pi*sqrt(x/2));

Script File Q5s.m


% Q5s.m clc;clear all;close all; N = input('Please enter N: '); x_0 = 0.1; x = zeros(1,N); x(1) = x_0; x1 = linspace(0,1.5,100); for i = 1 : N-1 [~, x(i+1)] = Q5f(x(i)); [~,y1] = Q5f(x(i)); V1 = y1*ones(1,2); H1 = linspace(x(i),x(i+1),2); [~,y2] = Q5f(x(i+1)); V2 = linspace(x(i+1),y2,2); H2 = x(i+1)*ones(1,2); Y1 = Q5f(x1); [~,Y2] = Q5f(x1); hold on plot(x1,Y1,x1,Y2,H1,V1,H2,V2) hold off end fprintf('The last x term is %7.8f \n',x(N))

Worksheet 9 Newtons Method Use func1.m and dfunc1.m as templates for f(x) and f(x).

newt_root.m is Dr. Tiris template for Newtons method.

The following is the Bisection Method template bisect2.m:


% bsect2.m clc;clear all;close all; xL = input('Input LHS value xL: '); xR = input('Input RHS value xR: '); if func1(xL)*func1(xR) > 0 disp('No root exists in chosen interval or the interval is too large') return elseif func1(xL) == 0 && func1(xR) == 0 disp('Both xL and xR are roots of the function') disp(['Encountered a root at x = ', num2str(xL),' and x = ', num2str(xR)]) return elseif func1(xL) == 0 disp('xL is a root of the function') disp(['Encountered a root at x = ', num2str(xL)]) return elseif func1(xR) == 0 disp('xR is a root of the function') disp(['Encountered a root at x = ', num2str(xR)]) return end n = 0; while abs(xL - xR) > 1e-10 n = n + 1; xM = (xL + xR)/2; if func1(xM)*func1(xL) < 0; xR = xM; elseif func1(xM)*func1(xR) < 0; xL = xM; elseif func1(xM) == 0 disp(['Encountered a root at xM = ', num2str(xM), '. This is an exact solution']) return end end fprintf(' \n') fprintf('%s %s %s %s 'n', 'x_L', 'func1(x_L)', 'x_R', 'func1(x_R)') fprintf(' \n') fprintf('%d %g %6.4f %g n, xL, func1(xL), xR, func1(xR))

%s \n',... %6.4f \n',...

Q8. The function file Q8F.m calculates two outputs w and w.

Use Q8S_Bsect.m for Bisection method if Q8F.m is used as the function file. Use Q8S_Newt.m for Newtons method if Q8F.m is used as the function file.

The function Q9Bisect inputs xL and xR and outputs the root and the number of iterations. The corresponding script file is Q9S.m (Q9F.m is the function file used for the fixed point iteration.

Worksheet 10 Use FuncT10.m as a template for typing the y = f(t,y).

Use EulSclr10.m as a template for Eulers method for one differential equation.

Use RK_Sclr10.m as a template for RK method for one differential equation.

Worksheet 11 Use FuncT11.m as a template for typing the y = f(t,y) for a system of DEs.

Use EulSys11.m as a template for Eulers method for a system of DEs.

Use RK_Sys11.m as a template for RK method for a system of DEs.

You might also like