03.05.2014 Views

Introduction to Numerical Math and Matlab ... - Ohio University

Introduction to Numerical Math and Matlab ... - Ohio University

Introduction to Numerical Math and Matlab ... - Ohio University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

24 LAB 7. SYMBOLIC COMPUTATIONS<br />

- internal variables are not visible outside the function.<br />

Script Programs:<br />

- There are no inputs <strong>and</strong> outputs.<br />

- A script program may use <strong>and</strong> change variables in the current workspace.<br />

Symbolic:<br />

> syms x y<br />

> f = 2*x^2 - sqrt(3*x)<br />

> subs(f,sym(pi))<br />

> double(ans)<br />

> g = log(abs(y)) . .................................. <strong>Matlab</strong> uses log for natural logarithm.<br />

> h(x) = compose(g,f)<br />

> k(x,y) = f*g<br />

> ezplot(f)<br />

> ezplot(g,-10,10)<br />

> ezsurf(k)<br />

> f1 = diff(f,’x’)<br />

> F = int(f,’x’) . .......................................... indefinite integral (antiderivative)<br />

> int(f,0,2*pi) .............................................................. definite integral<br />

> poly = x*(x - 3)*(x-2)*(x-1)*(x+1)<br />

> polyex = exp<strong>and</strong>(poly)<br />

> polysi = simple(polyex)<br />

> solve(f)<br />

> solve(g)<br />

> solve(polyex)<br />

Here are some sample questions from old tests. Some <strong>to</strong>pics that we covered are not represented by<br />

these questions, but are still fair game.<br />

1. Give <strong>Matlab</strong> comm<strong>and</strong>s <strong>to</strong> plot the function f(x) = x 2 − 5 on the interval [0, 8].<br />

2. Write a <strong>Matlab</strong> script program that will plot the functions x, x 2 , sinx, e x <strong>and</strong> lnx on the<br />

interval [0, 1].<br />

3. The function f(x) = x 2 −5 is continuous <strong>and</strong> f(0) < 0 < f(8), so it has a zero on the interval<br />

[0, 8]. Perform 3 iterations of the bisection method <strong>to</strong> narrow down this interval.<br />

4. Write a <strong>Matlab</strong> function program <strong>to</strong> do n steps of the bisection method for a function f<br />

with starting interval [a, b]. Let f, a, b <strong>and</strong> n be the inputs <strong>and</strong> the final x be the output.<br />

Include comments.<br />

5. For f(x) = x 2 − 5, do 2 iterations of the secant method, starting with x 0 = 1 <strong>and</strong> x 1 = 2.<br />

6. Write a <strong>Matlab</strong> function program <strong>to</strong> do New<strong>to</strong>n’s method for a function f until |f(x)| < <strong>to</strong>l.<br />

Let f, f ′ , x 0 <strong>and</strong> <strong>to</strong>l be the inputs <strong>and</strong> the final x be the output. Include comments.<br />

7. (a) We can approximate √ 2 by setting f(x) = x 2 − 2 <strong>and</strong> solving for f(x ∗ ) = 0. A<br />

reasonable starting guess is x 0 = 1. Do two iterations of New<strong>to</strong>n’s method <strong>to</strong> get a<br />

better approximation for √ 2.<br />

(b) Write a <strong>Matlab</strong> function with inputs z <strong>and</strong> <strong>to</strong>l that computes √ z <strong>to</strong> <strong>to</strong>lerance <strong>to</strong>l<br />

using New<strong>to</strong>n’s method, <strong>and</strong> returns the result. Include comments. (It is illegal <strong>to</strong> use<br />

sqrt in your program.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!