ps8solnsSp06

ps8solnsSp06 - %Engineering 6, Spring 2006, Problem 8.1...

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

View Full Document Right Arrow Icon
%Engineering 6, Spring 2006, Problem 8.1 Solution %(Taylor series of exp(x) using for loop) %Suppress extra lines in output and set fixed short "good" display format compact ; format short g ; clc; %Clear command window and variables clear; %Get value of x and number of terms to use from user disp( 'Calculate exp(x) using Taylor series' ); x = input( 'Enter value of x: ' ); numterms = input( 'Enter number of terms to use: ' ); %Each term in the Taylor series has the form x^n/n!, where n %starts at 0. (Note that 0! is defined to be 1.) %Loop to calculate sum of Taylor series terms (note that %if there are 10 terms, the last value of n is 10-1, and so on). Taylorsum = 0; %Initialize sum value to 0 for n=0:(numterms-1) Taylorsum = Taylorsum + x^n/factorial(n); end %Calculate actual value of exp(x) and rel. percentage error actualvalue = exp(x); relativeerror = 100*abs(actualvalue-Taylorsum)/actualvalue; %Display results disp( 'Taylor series value of exp(x):' ); disp(Taylorsum); disp( 'Actual value of exp(x):' ); disp(actualvalue); disp( 'Relative percentage error:' ); fprintf( '%.3f\n' ,relativeerror); Displayed results: Calculate exp(x) using Taylor series Enter value of x: 10 Enter number of terms to use: 5 Taylor series value of exp(x): 644.33 Actual value of exp(x): 22026 Relative percentage error: 97.075 Calculate exp(x) using Taylor series Enter value of x: 10 Enter number of terms to use: 10 Taylor series value of exp(x): 10087 Actual value of exp(x): 22026
Background image of page 1

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

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

Page1 / 5

ps8solnsSp06 - %Engineering 6, Spring 2006, Problem 8.1...

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

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