hw11problem4

hw11problem4 - has been achieved current_term =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
% ENGR 126 hw11_prob5.m % This script uses looping to approximate the value of exp(x) using a Taylor series approximation. % The value of x and the desired accuracy of the approximation are supplied by the user. %---- Inputs ---- x = input('Enter x = '); desired_accuracy = input('Enter desired level of accuracy = '); n = 0; % series counter current_term = 0; % initialize current term series_sum = 0; % initialize taylor series summation %---- Calculations ---- exp_diff = abs(exp(x)-series_sum); % calculate initial accuracy while exp_diff >= desired_accuracy % use while loop to check if accuracy
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: has been achieved current_term = x^n/factorial(n); % calculate current taylor series term series_sum = series_sum + current_term; % summation of all terms calculated exp_diff = abs(exp(x)- series_sum); % accuracy calculation n = n+1; % increment index end n %---- Outputs ----fprintf('exp(%f)= %.5f \n',x,series_sum) % Enter x = 2 % Enter desired level of accuracy = 0.001 % exp(2.000000)= 7.38871 % Enter x = -4 % Enter desired level of accuracy = 0.0001 % exp(-4.000000)= 0.01836 % Enter x = 12.3 % Enter desired level of accuracy = 0.00005 % exp(12.300000)= 219695.98865...
View Full Document

Ask a homework question - tutors are online