Lab11

advertisement
ES117 Lab 11- Loops
Use a for loop to solve the following problems:
1. Create a table that converts inches to feet.
%% Exercise 9.1.1
inches = 0:3:24;
for k=1:length(inches)
feet(k) = inches(k)/12;
end
table=[inches',feet']
%% Exercise 9.2.1
inches = 0:3:24;
k=1;
while k<=length(inches)
feet(k) = inches(k)/12;
k=k+1;
end
disp(' Inches Feet');
fprintf(' %8.0f %8.2f \n',[inches;feet])
2. Consider the following matrix of values:
x=[ 45, 23, 17, 34, 85, 33 ]
How many values are greater than 30? (Use a counter.)
%% Exercise 9.1.2
x = [ 45,23,17,34,85,33];
count=0;
for k=1:length(x)
if x(k)>30
count = count+1;
end
end
fprintf('The are %4.0f values greater than 30 \n',count)
%% Exercise 9.2.2
x = [ 45,23,17,34,85,33];
k=1;
count = 0;
while k<=length(x)
if x(k)>=30;
count = count +1;
end
k=k+1;
end
fprintf('There are %4.0f values greater than 30 \n',count)
3. Repeat Exercise 2, this time using the find command.
%% Problem 9.1.3
num = length(find(x>30));
fprintf('The are %4.0f values greater than 30 \n',num)
4. Use a for loop to sum the elements of the matrix in Problem 2.
%% Exercise 9.1.4
total = 0;
for k=1:length(x)
total = total + x(k);
end
disp('The total is: ')
disp(total)
sum(x)
%% Exercise 9.2.4
k=1;
total = 0;
while k<=length(x)
total = total + x(k);
k=k+1;
end
disp(total)
sum(x)
5. Use a for loop to create a vector containing the first 10 elements in the harmonic series, i.e.,
1/1, ½, 1/3 , ¼, 1/5, ... 1/10
%% Exercise 9.1.5
for k=1:10
x(k)=1/k
end
%% Exercise 9.2.5
k=1;
while k<=10
x(k)=1/k;
k=k+1;
end
x
6. Use a for loop to create a vector containing the fi rst 10 elements in the alternating harmonic
series, i.e.,
1/1, -1/2, 1/3, -1/4, 1/5,... -1/10
%% Exercise 9.1.6
for k=1:10
x(k)=(-1)^(k+1)/k
end
%% Exercise 9.2.6
k=1;
while k<=10
x(k) = (-1)^(k+1)/k;
k = k+1;
end
x
7. Create a new function called fact2 that uses a while loop to find N !. Include an if statement to
check for negative numbers and to confi rm that the input is a scalar.
function output = fact2(x)
%This function uses a while loop to %find x!
%The input must be a positive integer
if(length(x)>1 | x<0)
output = 'The input must be a positive integer';
else
%Initialize the running product
a = 1;
%Initialize the counter
k = 1;
while k<x
%Increment the counter
k = k + 1;
%Calculate the running product
a = a*k;
end
output = a;
end
8.
%% Example 9.5
% Calculating the Alternating Harmonic Series
clear,clc
% Define the first two elements in the series
y(1) = 1;
y(2) = -1/2;
% Calculate the first two cumulative sums
total(1) = y(1);
total(2) = total(1) + y(2);
k=3;
while (abs(total(k-1) - total(k-2)) > .001)
y(k) = (-1)^(k+1)/k;
total(k) = total(k-1) + y(k);
k=k+1;
end
fprintf('The sequence converges when the final element is equal to %8.3f \n', y(k-1));
fprintf('At which point the value of the series is %5.4f \n',total(k-1))
fprintf('This compares to the value of the ln(2), %5.4f \n', log(2));
fprintf('The sequence took %3.0f terms to converge \n',k)
%% Plot the results
semilogx(total)
title('Value of the Alternating Harmonic Series')
xlabel('Number of Terms')
ylabel('Sum of the terms')
1. Write a script file to determine the sum of the series ∑
.
2.
a) Write a script file to compute the sum of the first 15 terms in the series
.
b) Write a script file to find the number of terms required in order to the sum of the series
not to exceed 10,000.
3. Write a script file to determine how long it will take to accumulate at least 10,000 TL in a
bank account if you deposit 500 TL initially and 500 TL at the end of each year, if the account
pays 5 percent annual interest.
clc,clear
n=1;
total=500;
while total<10000
new=total*(1+0.05)^n
total=500 + new
n=n+1;
end
disp(n)
4. Write a MATLAB script to calculate value of e using Taylor series approximation, which
is given below, for 10 terms. Compare your result to exp(1) in MATLAB.
1 1 1 1
e  1      ...
1! 2! 3! 4!
Rewrite the program to determine how many terms needed to calculate e with 10-3 precision.
5.
a. Write a function to find the number of digits of a given integer number.
b. Write a function to sum up digits of a given integer.
6. Write a function that calculates summation of elements of an array such as sum(A).
7. Write a function that finds and prints ( row by row) the transpose of a 3x2 matrix given by
the user. Below an example is given for your information.
8. Write a MATLAB code for the following pseudo-code algorithm:
Set running multiplication to 1
Input a value from the keyboard
While the input value is not equal to 0
Multiply the number with the running multiplication
Input the next number
Print the multiplication
9. HW. Write a MATLAB program that calculates and prints the sum of even integers from 2
to 30.
Download