Quiz6_Solution - functions (e.g. factorial()) Solution: %...

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

View Full Document Right Arrow Icon
Quiz 6: For Loops Name: ASE 201 Write a MATLAB script (BE SURE TO USE MATLAB NOTATION!) that will calculate e x using n terms of the Taylor Series expansion given by e x = 1 + x 1! + x 2 2! + x 3 3! + · · · = 1 + X k =2 x k - 1 ( k - 1)! and plot the value at each successive term (k vs. e x ). Your script should complete the following tasks: 1. Read-in a values for x and n . 2. Calculate e x using n terms of the Taylor series expansion (Using only 1 term would result in e x = 1). 3. Build-up the arrays of k and e x . 4. Display the value of e x for n terms and plot the value at each term. Be sure to include labels on your plot. Note that you are required to use a FOR loop, and you are not allowed to use any MATLAB
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: functions (e.g. factorial()) Solution: % quiz6.m clear close all x = input(Enter a value for x: ); n = input(Enter the number of terms to calculate e^x: ); k_p = 1; ex_p = 1; fact = 1; summ = 1; for k = 2:n fact = fact*(k-1); summ = summ + x^(k-1) / fact; k_p = [k_p,k]; ex_p = [ex_p,summ]; end fprintf(\n e^( %g ) = %.10f using %g terms of the Taylor Series \n,x,summ,n) plot(k_p,ex_p,LineWidth,2) xlabel(k,FontSize,12) ylabel(e^x,FontSize,12)...
View Full Document

Ask a homework question - tutors are online