Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

matlab, please help Q22 based on picture below, 22) Write a script to calculate the product of all the elements of a by using (5.6),

image text in transcribed

image text in transcribed

matlab, please help Q22 based on picture below,

22) Write a script to calculate the product of all the elements of a by using (5.6), i.e., calculating the sum of the logarithms, and return the result as Pn = fx10" where |f| [1, 10) and f can be positive or negative, and where m is an integer. Of course, f = 0 if one of the elements of a is 0. The code must check each element to determine if it is positive, negative, or zero, and also keep track of the overall sign of the product. In addition, if a zero element is found, the loop should immediately exit with f = m = 0 (so there is a unique solution). The name of the script should be logprod.m, the input a, and the outputs f and m. Multiplying Elements of a Vector Multiplying all the elements of a vector does not occur nearly as often as summing them, but it is worth considering in its own right. The code for multiplying all the elements of the vector r is % script m-file: myprod % input: % output: P $ DON'T USE prod FOR THE VARIABLE NAME p = 1; % p begins before the first element for j = 1:length(x) p = p*x(j); x end This code works even if is empty since it returns 1, and so does >> prod([]) ans = 1 Simplest Code: The equivalent vectorized MATLAB code is >> s = prod(x); Incidentally, a product of terms can grow or decay much faster than a sum of terms and, before you know it, the result is 0 or +Inf or NaN as can be seen in Problem 5.6-2. This difficulty can usually be avoided by replacing Pa = II. by log|P.nl = log |ai (5.6) 72 i=1 i=1 as long as none of the terms are 0 - of course, if one or more terms are 0 the product is immediate. The result is then Pr = fx10m in scientific notation where If [1,10) and f can be positive or negative, and where m is an integer; of course, if one of the elements is 0, f = 0. It is the code's responsibility to determine the correct sign on f, and also to check if any of the elements are 0, in which case Pr can be immediately set to 0. This can be coded in Problem 5.6-22. 22) Write a script to calculate the product of all the elements of a by using (5.6), i.e., calculating the sum of the logarithms, and return the result as Pn = fx10" where |f| [1, 10) and f can be positive or negative, and where m is an integer. Of course, f = 0 if one of the elements of a is 0. The code must check each element to determine if it is positive, negative, or zero, and also keep track of the overall sign of the product. In addition, if a zero element is found, the loop should immediately exit with f = m = 0 (so there is a unique solution). The name of the script should be logprod.m, the input a, and the outputs f and m. Multiplying Elements of a Vector Multiplying all the elements of a vector does not occur nearly as often as summing them, but it is worth considering in its own right. The code for multiplying all the elements of the vector r is % script m-file: myprod % input: % output: P $ DON'T USE prod FOR THE VARIABLE NAME p = 1; % p begins before the first element for j = 1:length(x) p = p*x(j); x end This code works even if is empty since it returns 1, and so does >> prod([]) ans = 1 Simplest Code: The equivalent vectorized MATLAB code is >> s = prod(x); Incidentally, a product of terms can grow or decay much faster than a sum of terms and, before you know it, the result is 0 or +Inf or NaN as can be seen in Problem 5.6-2. This difficulty can usually be avoided by replacing Pa = II. by log|P.nl = log |ai (5.6) 72 i=1 i=1 as long as none of the terms are 0 - of course, if one or more terms are 0 the product is immediate. The result is then Pr = fx10m in scientific notation where If [1,10) and f can be positive or negative, and where m is an integer; of course, if one of the elements is 0, f = 0. It is the code's responsibility to determine the correct sign on f, and also to check if any of the elements are 0, in which case Pr can be immediately set to 0. This can be coded in Problem 5.6-22

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions