MATLAB: A Practical Introduction to Programming and Problem Solving
MATLAB: A Practical Introduction to Programming and Problem Solving
5th Edition
ISBN: 9780128154793
Author: Stormy Attaway Ph.D. Boston University
Publisher: Elsevier Science
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 10, Problem 10.1P
Expert Solution & Answer
Check Mark
To determine

To write:

A function that will receive a value for r and calculate and return the sum of the geometric series and if a second argument is passed to the function, then the function generates a random integer for n.

Answer to Problem 10.1P

Solution:

The function file is,

%MATLAB code to create a function that will receive a value for r and calculate and return the sum of the geometric series and if a second argument is passed to the function, then the function generates a random integer for n.

%function file

function sum = geomser(r, varargin)

%define the function geomser.

arg = nargin;

if arg == 2

%if arguments are two then it will generate proper sum.

n = varargin{1};

%if only second argument is present then it will take a random integer

%for n.

else

n = randi([5, 30]);

end

s = 1;

for j = 1:n

s = s + r.^j;

end

sum = s;

end

%end of function file

%The function file should be placed in the same folder.

Explanation of Solution

The formula for to calculate the sum of a geometric series is given as,

S=1+r+r2+r3+r4+...+rn

Substitute 3 for r and 5 for n in the above formula.

S=1+(3)+(3)2+(3)3+(3)4+(3)5S=364

MATLAB Code:

%MATLAB code to create a function that will receive a value for r and calculate and return the sum of the geometric series and if a second argument is passed to the function, then the function generates a random integer for n.

%function file

function sum = geomser(r, varargin)

%define the function geomser.

arg = nargin;

if arg == 2

%if arguments are two then it will generate proper sum.

n = varargin{1};

%if only second argument is present then it will take a random integer

%for n.

else

n = randi([5, 30]);

end

s = 1;

for j = 1:n

s = s + r.^j;

end

sum = s;

end

%end of function file

%The function file should be placed in the same folder.

Save the MATLAB function with name, geomser.m in the current folder. Execute the function file by typing the function name at the command window to generate output.

Result:

The result is,

MATLAB: A Practical Introduction to Programming and Problem Solving, Chapter 10, Problem 10.1P

Therefore, the result is stated above.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Children develop different patterns of attachment based on interactions with their caregivers at young age (Wikipedia). Certain study attempt to find the association between Child Care and Attachment Pattern Classification. Below table summarizes the collected data. Hours in Child Care Low Moderate High Attachment Pattern (0-3 hours) (4-19 hours) (20-54 hours) Secure 24 35 5 Anxious 11 10 8 Carryout an appropriate test to see if there is an association between amount of time spend on Child Care and Attachment Pattern of the children. 6. In order to answer the same research question as in previous parts in this problem, can we do a hypothesis test for comparing two independent proportions while keeping the existing categories as they are? Explain your answer rather than simply saying "yes" or "no".
This questions has multiple subparts. For your convenience, the description of the problem is provided with each component. Children develop different patterns of attachment based on interactions with their caregivers at young age (Wikipedia). Certain study attempt to find the association between Child Care and Attachment Pattern Classification. Below table summarizes the collected data. Hours in Child Care Low Moderate High Attachment Pattern (0-3 hours) (4-19 hours) (20-54 hours) Secure 24 35 5 Anxious 11 10 8 Carryout an appropriate test to see if there is an association between amount of time spend on Child Care and Attachment Pattern of the children. 1. The null hypothesis is [Select] and the alternative hypothesis is [Select] [Select] 2. The test statistic is 3. Degrees of freedom 4. P-value is [Select] True proportion of children with secure pattern when low number of hours in child care and true proportion of children with anxious pattern when low number of hours in child Hours…
Does money buy happiness? This is an age-old question. A random sample of 1993 subjects were asked to rate their happiness (Not too happy, Pretty happy, or Very happy) and also report their level of income (Above average, average, below average). The table of the observations is presented below. Income Not too happy Pretty happy Very happy Total Above Average 26 233 164 423 Average 117 473 293 883 Below Average 172 383 132 687 Total 315 1089 589 1993 1. Which variable is the response and which is the explanatory variable? [Select] 2. What are the null and alternative hypotheses for testing if there is a relationship between level of income and happiness? [Select] and H₁ is [Select] Ho is 3. What is the chi-square test statistic? [Select] 4. Which cell contributed the most to the overall chi-square test statistic? [Select] 5. What are the degrees of freedom? [Select] 6. What is the decision about Ho? [Select]
Knowledge Booster
Background pattern image
Statistics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, statistics and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage
Sequences and Series Introduction; Author: Mario's Math Tutoring;https://www.youtube.com/watch?v=m5Yn4BdpOV0;License: Standard YouTube License, CC-BY
Introduction to sequences; Author: Dr. Trefor Bazett;https://www.youtube.com/watch?v=VG9ft4_dK24;License: Standard YouTube License, CC-BY