MATLAB: A Practical Introduction to Programming and Problem Solving
MATLAB: A Practical Introduction to Programming and Problem Solving
5th Edition
ISBN: 9780128163450
Author: ATTAWAY
Publisher: ELSEVIER
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 10, Problem 10.1P
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.

Expert Solution & Answer
Check Mark

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
You have been hired as an intern to run analyses on the data and report the results back to Sarah; the five questions that Sarah needs you to address are given below.   Does there appear to be a positive or negative relationship between price and screen size? Use a scatter plot to examine the relationship. Determine and interpret the correlation coefficient between the two variables. In your interpretation, discuss the direction of the relationship (positive, negative, or zero relationship). Also discuss the strength of the relationship. Estimate the relationship between screen size and price using a simple linear regression model and interpret the estimated coefficients. (In your interpretation, tell the dollar amount by which price will change for each unit of increase in screen size). Include the manufacturer dummy variable (Samsung=1, 0 otherwise) and estimate the relationship between screen size, price and manufacturer dummy as a multiple linear regression model. Interpret the…
Does there appear to be a positive or negative relationship between price and screen size? Use a scatter plot to examine the relationship. How to take snapshots: if you use a MacBook, press Command+ Shift+4 to take snapshots. If you are using Windows, use the Snipping Tool to take snapshots. Question 1: Determine and interpret the correlation coefficient between the two variables. In your interpretation, discuss the direction of the relationship (positive, negative, or zero relationship). Also discuss the strength of the relationship.  Value of correlation coefficient:   Direction of the relationship (positive, negative, or zero relationship):   Strength of the relationship (strong/moderate/weak): Question 2: Estimate the relationship between screen size and price using a simple linear regression model and interpret the estimated coefficients. In your interpretation, tell the dollar amount by which price will change for each unit of increase in screen size. (The answer for the…
In this problem, we consider a Brownian motion (W+) t≥0. We consider a stock model (St)t>0 given (under the measure P) by d.St 0.03 St dt + 0.2 St dwt, with So 2. We assume that the interest rate is r = 0.06. The purpose of this problem is to price an option on this stock (which we name cubic put). This option is European-type, with maturity 3 months (i.e. T = 0.25 years), and payoff given by F = (8-5)+ (a) Write the Stochastic Differential Equation satisfied by (St) under the risk-neutral measure Q. (You don't need to prove it, simply give the answer.) (b) Give the price of a regular European put on (St) with maturity 3 months and strike K = 2. (c) Let X = S. Find the Stochastic Differential Equation satisfied by the process (Xt) under the measure Q. (d) Find an explicit expression for X₁ = S3 under measure Q. (e) Using the results above, find the price of the cubic put option mentioned above. (f) Is the price in (e) the same as in question (b)? (Explain why.)
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