Homework 2

Homework 2 - >>%This program uses Newtons Method to...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: >> %This program uses Newtons Method to find the root Po of our function f% >> Po = 1.5; %Since [1,2], choose 1.5% >> f = inline ('exp(x) + 2^-x+2*cos(x)-6') f = Inline function: f(x) = exp(x) + 2^-x+2*cos(x)-6 >> g= inline ('exp(x) - 2^(-x)*log(2)-2*sin(x)') g = Inline function: g(x) = exp(x) - 2^(-x)*log(2)-2*sin(x) >> %f is the original function and g is the derivative of f% >> i=1; >> max =4; %Max is the maximum number of iterations% >> while i <= max p = Po-f(Po)/g(Po); i=i+1; Po=p format long end Po =1.9565 Po =1.84153306104206 Po =1.82950601320365 Po =1.82938361449417 >> %This program uses Newtons Method to find the root Po of our function f% >> Po=1.4; %Initial guess is 1.4% >> f = inline ('log (x-1) + cos(x-1)') f = Inline function: f(x) = log (x-1) + cos(x-1) >> g = inline ('1/(x-1) - sin(x-1)') g = Inline function: g(x) = 1/(x-1) - sin(x-1) >> %g is the derivative of f% >> i=1; >> max = 3; %Max is the maximum number of iterations% >> while i <= max p = Po-f(Po)/g(Po); i=i+1; Po=p format long end Po =1.39773983531443 Po =1.39774847583162 Po =1.39774847595875 >> %This program uses Newtons Method to find the root Po of our function f% >> %p1 and p2 are initial guesses% >> %p1 will be within the interval 2 and 3% >> %p2 will be within the interval 3 and 4% >> p1 =2.5; >> p2=3.5; >> f = inline('2*x*cos(2*x)-(x-2)^2') f = Inline function: f(x) = 2*x*cos(2*x)-(x-2)^2 >> g = inline('2*cos(2*x)-4*x*sin(2*x)-2*x+4')%g is the derivative of f% g = Inline function: g(x) = 2*cos(2*x)-4*x*sin(2*x)-2*x+4 >> max =3; %max is maximum number of iterations% >> i=1; >> while i <= max pa = p1-f(p1)/g(p1); %pa is when working in p1% pb = p2-f(p2)/g(p2); %pb is when working in p2% p1 =2.37240732118090 p2 =3.78319116470837 p1 =2.37068782574746 p2 =3.72416540142925 p1 =2.37068691766252 p2 =3.72211549727338 >> %This program uses Newtons Method to find the root Po of our function f% %p1 and p2 are initial guesses% p1 = 1.5; %p1 lies between 1 and 2% p2 = 3; %p2 lies between e^1 and 4% >> f = inline ('(x-2)^2-log(x)')...
View Full Document

This note was uploaded on 05/01/2009 for the course PSTAT 120A taught by Professor Mackgalloway during the Spring '08 term at UCSB.

Page1 / 10

Homework 2 - >>%This program uses Newtons Method to...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online