hw12_solutions

hw12_solutions - Section2.1 1d x 3 + 3x + 2 = 2 + x(3 + x 2...

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

View Full Document Right Arrow Icon
Section2.1 1d x 3 + 3x + 2 = 2 + x(3 + x 2 ) 4 & 9 (1d) This code below evaluates polynomial and its first derivative using both horner’s rule, it also evaluates the polynomial using naive evaluation. After 4000 iterations, horner’s rule uses 0.002004 seconds whiles naive evaluation uses 0.003417 seconds which shows that horner’s rule is more efficient. Just copy it and paste it in Matlab’s command window. The matlab code is as below %%%%%%%%%%%% section 2.1 4 and 9 %%%%%%%%%%%%%% % x^3 + 3x + 2 % Evaluate this polynomial using Horner¡¯s rule. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% close all clc clear set(0, 'DefaultFigureWindowStyle' , 'docked' ) %% polynomial coefficients a = [2 3 0 1]; x = 0:0.001:4; N = length(a); M = length(x); %% prob 4.1 % evaluate polynomial using horner's rule polyhorner = zeros(size(x)); display( 'using horner rule' ) tic for m = 1:M %%%%%%%%%% horner's rule %%%%%%%%%%%%%%% px = a(N); for n = N-1:-1:1 px = a(n) + px*x(m); end polyhorner(m) = px; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end toc figure(1); plot(x,polyhorner) grid on xlabel( 'x' ) title( 'evaulated polynomial value using horner rule' ) %% prob 4.2 % evaluate polynomial's first derivative using horner's rule polyhorner_deri = zeros(size(x)); for m = 1:M %%%%%%%%%% horner's rule %%%%%%%%%%%%%%% dp = (N-1)*a(N); for n = N-1:-1:2 dp = (n-1)*a(n) + dp*x(m); end polyhorner_deri(m) = dp; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end figure(2); plot(x,polyhorner_deri) grid on xlabel( 'x' ) title( 'evaulated polynomial first derivative value using horner rule' )
Background image of page 1

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

View Full DocumentRight Arrow Icon
%% prob 9 % in addition to prob 4, prob 9 requires the naive evaluation polynaive = zeros(size(x)); display( 'using naive evaluation' ) tic for m = 1:M %%%%%%%%%% horner's rule %%%%%%%%%%%%%%% np = a(1); for n = 2:N np = np + a(n)*x(m)^(n-1); end polynaive(m) = np;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2012 for the course MAE 107 taught by Professor Rottman during the Spring '08 term at UCSD.

Page1 / 7

hw12_solutions - Section2.1 1d x 3 + 3x + 2 = 2 + x(3 + x 2...

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

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