Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

Copy the m-file Example-2-2.m to a new file example2.m and modify the new m-file to compute exp(x) for 0: .2: 10]. You should use no loops to compute all values of r: write the program using array processing You will still need a loop for the algorithm. Use help to find out how to use fprintf for arrays. Make sure you use the method employed in Example-2-2.m to compute the exponential. Print out your m-file and program output to be turned in

Example 2.2: Nested Loops
% Example_2_2.m
% This program illustrates the use of nested loops, i.e.
% an inner 'for' loop inside an outer 'for' loop.
% The program calculates e^x by both MATLAB's 'exp'
% command (variable 'ex2'), and by a Taylor series
% expansion (variable 'ex1'), where -0.5 < x < 0.5.
% The outer 'for' loop is used to determine the x
% values. The inner loop is used to determine the Taylor
% series method for evaluating e^x. In this example,
% term(n+1) is obtained by multiplying term(n) by x/n.
% The variable 'term' is established as a vector so that
% MATLAB's built-in 'sum' function can be used to sum
% all the terms calculated in the Taylor series method.
% A maximum of fifty terms is used in the series.
% Program output is sent both to the screen and to a
% file. By printing the output to a file, you can easily
% edit the output file to line up column headings,
% etc. (which you can't do when printing to the screen).
% Note: e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ...
clear; clc;
xmin=-0.5; dx=0.1;
fo=fopen('output.txt','w');
% Table headings
fprintf(' x ex1 ex2 ');
fprintf('------------------------------------- ');
fprintf(fo,' x ex1 ex2 ');
fprintf(fo,'------------------------------------- ');
for i=1:11
x=xmin+(i-1)*dx;
ex2=exp(x);
term(1)=1.0;
for n=1:49
term(n+1)=term(n)*x/n;
if abs(term(n+1)) <= 1.0e-7
break;
end
end
ex1 = sum(term);
fprintf('%5.2f %10.5f %10.5f ',x,ex1,ex2);
fprintf(fo,'%5.2f %10.5f %10.5f ',x,ex1,ex2);
end
fclose(fo);
------------------------------------------------------------

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.