Programs

# Programs - Newton method function x = mynewton(f,f1,x0,n Solves f(x = 0 by doing n steps of Newton’s method starting at x0 Inputs f the function

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

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: Newton method function x = mynewton(f,f1,x0,n) % Solves f(x) = 0 by doing n steps of Newton’s method starting at x0. % Inputs: f -- the function, input as an inline % f1 -- it’s derivative, input as an inline % x0 -- start ing guess, a number % n -- the number of steps to do % Output: x -- the approximate solution format long % prints more digits format compact % makes the output more compact x = x0; % set x equal to the initial guess x0 for i = 1:n % Do n times x = x - f(x)/f1(x) % Newton’s formula, prints x too end ************************************** Newton with tolerence function x = mynewtontol(f,f1,x0,tol) % Solves f(x) = 0 by doing Newton’s method starting at x0. % Inputs: f -- the function, input as an inline % f1 -- it’s derivative, input as an inline % x0 -- start ing guess, a number % tol -- desired tolerance, goes until | f(x) |<tol % Output: x -- the approximate solution x = x0; % set x equal to the initial guess x0 y = f(x); while abs(y) > tol % Do until the tolerence is reached. x = x - y/f1(x); % Newton’s formula y = f(x); end ************************************** Bisection Method function [x e] = mybisect(f,a,b,n) % function [x e] = mybisect(f,a,b,n) % Does n iterations of the bisection method for a function f % Inputs: f -- an inline function % a,b -- left and right edges of the interval % n -- the number of bisections to do. % Outputs: x -- the estimated solution of f(x) = 0 % e -- an upper bound on the error format long c = f(a); d = f(b); if c*d > 0.0 error(’Function has same sign at both endpoints.’) end disp(’ x y’) for i = 1:n x = (a + b)/2; y = f(x); disp([ x y]) if y == 0.0 % solved the equation exactly e = 0; break % jumps out of the for loop end if c*y < 0 b=x; else a=x; end end e = (b-a)/2; ************************************** Find roots (sign change) function [a,b] = myrootfind(f,a0,b0) % function [a,b] = myrootfind(f,a0,b0) % Looks for subintervals where the function changes sign % Inputs: f -- an inline function % a0 -- the left edge of the domain % b0 -- the right edge of the domain % Outputs: a -- an array, giving the left edges of subintervals % on which f changes sign % b -- an array, giving the right edges of the subintervals n = 1001; % number of test points to use a = ; % start empty array b = ; x = linspace(a0,b0,n); y = f(x); for i = 1:(n-1) if y(i)*y(i+1) < 0 % The sign changed, record it a = [a x(i)]; b = [b x(i+1)]; end end if a == warning(’no roots were found’) end *************************************** Secant Method function x = mysecant(f,x0,x1,n) format long % prints more digits format compact % makes the output more compact % Solves f(x) = 0 by doing n steps of the secant method starting with x0 and x1....
View Full Document

## This note was uploaded on 01/15/2011 for the course MATH 345 taught by Professor Staff during the Spring '08 term at Ohio State.

### Page1 / 3

Programs - Newton method function x = mynewton(f,f1,x0,n Solves f(x = 0 by doing n steps of Newton’s method starting at x0 Inputs f the function

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

View Full Document
Ask a homework question - tutors are online