Question

1. Create a function that calculates N!, where N is an input variable to the function. Use a for loop and calculate the factorial without using the factorial function built into MATLAB.

2. Prof. Sky wants to retire when his retirement account first reaches or exceeds $2,000,000. He deposits $20,000 at the beginning of every year into the account which pays 9% interest per year, compounded annually. Create a function that calculates how many years Prof Moss will have to teach before he can retire? (Hint: assume a $20,000 initial balance for year 0.)

3.Generate a vector of 1 million random numbers that are normally distributed. Plot the points in a histogram with 100 bins.

ANSWER ALL QUESTIONS

Answer #1

Matlab Code:

function [ fact ] = factorialNumber( N )

%UNTITLED2 Summary of this function goes here

% Detailed explanation goes here

fact = 1;

for i = 1:1:N

fact = fact*i;

end

------------------

Sample Run:

>> factorialNumber( 4 )

ans =

24

>> factorialNumber( 5 )

ans =

120

>>

------------------------------------------

Matlab Code:

r = rand(1,1e6);

hist(r,100)

xlabel 'bins'

ylabel 'Random Value'

title 'Histogram of Random Numbers'

-------------------------------

Result:

---------------------------------------------------------

Matlab Code :

clc;clear;

year = 0;

amount = 20000;

while (amount < 2000000)

amount = amount + ((amount*9)/100);

year = year + 1;

end

sprintf('No of Years Reuired = %d',year)

----------------------------------------

output :

ans =

No of Years Reuired = 54

-----------------

Dont Forget to hit like....

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 13 minutes ago

asked 18 minutes ago

asked 18 minutes ago

asked 25 minutes ago

asked 26 minutes ago

asked 30 minutes ago

asked 35 minutes ago

asked 36 minutes ago

asked 36 minutes ago

asked 42 minutes ago

asked 49 minutes ago

asked 56 minutes ago