Me318m HW3
Me318m HW3
Me318m HW3
Session #: 18495
Class: M E 318M
HW 3
Problem 1
Explanation
I create a blank array called Fibonacci1 and set variable Limit
to true. I then ask the user for the first number of the
Fibonacci array and store that in the first spot in the array.
I then ask for the second number of Fibonacci array and store
that in the second spot of the array. I then ask for the limit
and store as LimitValue. I then set Index to be initially 3. I
then have a while loop that continues to loop as long as Limit
is true. In the loop the CurrentNum is set has the number from
the array two positions before plus the number from the array 1
before the Index value. Then it checks if CurrentNum is greater
than or equal too the LimitValue and if it is then Limit is set
to false and the while loop ends. If it is not, the position of
Index in the array is set as CurrentNum. This is continued
until while loop ends.
Code
Fibonacci1= [];
Limit=true;
Index = 3;
while Limit==true
CurrentNum = Fibonacci1(Index-1)+Fibonacci1(Index-2);
if CurrentNum>=LimitValue
Limit=false;
else
Fibonacci1(Index)=CurrentNum;
end
Index=Index+1;
end
disp(['Here is the Fibonacci array: ' num2str(Fibonacci1)])
Output/Command Line
>> Homework3Problem1
Enter First element of Fibonaci array-> 1
Enter First element of Fibonaci array-> 2
Enter limit here-> 20
Here is the Fibonacci array: 1 2 3 5 8 13
>> Homework3Problem1
Enter First element of Fibonaci array-> 0
Enter First element of Fibonaci array-> 1
Enter limit here-> 15
Here is the Fibonacci array: 0 1 1 2 3 5 8 13
Results
N/A
Problem 2
Explanation
Array is initialized as a blank array. The user is then asked
how many integers they want in their array and this is saved as
NumOfInt. Isint is set as true. Then a for loop starts loop
between 1 and NumOfInt. In the loop, The user is asked for an
integer. If the number entered minus the rounded version of the
rounded number is not equal to zero, then it is not an integer
and sets IsInt to false. A while loop runs if IsInt is false
until IsInt is not false. In the while loop, the user is told
that their number is not an integer, told to enter a different
integer, and then checks if it passes the previous if statement
and if it does, sets IsInt to true. After the while loop is
ended, the array position of the index is set to the entered
integer. This is repeated through the for loop until it is done
with its iterations. After this, the array is displayed. Then
the sorting even and odds section starts. Two arrays, OddArray
and EvenArray, are initiated as empty arrays. OddCount and
EvenCount are each initiated as 1. There is then a for
statement that iterates from Index is 1 to NumOfInt. In the
loop, the code checks if the number in the Index position of
the array divided by two and then subtracted by that number
divided by 2 and then rounded is 0. If it is 0, the position of
EvenCount in the EvenArray is set to the number in position
Index of Array and 1 is added to EvenCount. If it is not the
position of OddCount in the OddArray is set to the number in
position Index of Array and 1 is added to OddCount. Then both
arrays are displayed.
Code
Array=[];
Output/Command Line
>> Homework3Problem2
How many integers do you want in the array? 5
Please enter an integer-> 1
Please enter an integer-> -4
Please enter an integer-> 2.7
Hey that is not an integer – try again!
Please enter an integer-> -8.9
Hey that is not an integer – try again!
Please enter an integer-> 10000
Please enter an integer-> -57
Please enter an integer-> 227
This is the array 1 -4 10000 -57 227
This is the array of even integers: -4 10000
This is the array of odd integers: 1 -57 227
Results
N/A
Problem 3
Explanation
The code sets Matrix1 to values shown and Matrix2 to values
shown. Matrix 3 is then Matrix 1 plus the matrix multiplication
of Matrix2 and the transpose of Matrix2. DetMatrix3 is then
found using the det.m function which performs the determinate
operations.
Code
Matrix1 = [1 2 7; 4 3 1; 2 1 4]
Matrix2 = [1 7; 3 5; 5 2]
DetMatrix3= det(Matrix3)
Output/Command Line
>> Homework3Problem3
Matrix1 =
1 2 7
4 3 1
2 1 4
Matrix2 =
1 7
3 5
5 2
Matrix3 =
51 40 26
42 37 26
21 26 33
DetMatrix3 =
2.3850e+03
Results
N/A