Me318m HW4
Me318m HW4
Me318m HW4
Session #: 18495
Class: M E 318M
HW 4
Problem 1a
Explanation
This code creates a function that takes in an array and then
iterates through that array to check if each element is odd or
even. If it is odd then 1 is added to OddNums and if even 1 is
added to EvenNums. The values of OddNums and EvenNums are then
displayed after the iterations are complete.
Code
function [OddNums, EvenNums]= MyOddEvenCountingFunction(Array)
ArrayLength= length(Array);
OddNums=0;
EvenNums=0;
for Index=1:ArrayLength
if round(Array(Index)/2)==(Array(Index)/2)
EvenNums=EvenNums+1;
else
OddNums=OddNums+1;
end
end
disp(['There are ' num2str(OddNums) ' odd numbers and '
num2str(EvenNums) ' even numbers.'])
end
MyOddEvenCountingFunction([2 8 11 15 12 56]);
MyOddEvenCountingFunction([8 55 2 16 389]);
Output/Command Line
There are 2 odd numbers and 4 even numbers.
There are 2 odd numbers and 3 even numbers.
Results
N/A
Problem 1b
Explanation
This code creates a function that takes in an array and then
iterates through that array to check if each element is
positive or negative. If it is positive then 1 is added to
PosNums and if negative 1 is added to NegNums. The values of
PosNums and NegNums are then displayed after the iterations are
complete.
Code
function [PosNums, NegNums]= MyPositiveNegativeFunction(Array)
ArrayLength= length(Array);
PosNums=0;
NegNums=0;
for Index=1:ArrayLength
if Array(Index)>0
PosNums=PosNums+1;
else
NegNums=NegNums+1;
end
end
disp(['There are ' num2str(PosNums) ' positive numbers and '
num2str(NegNums) ' negative numbers.'])
end
Results
N/A
Problem 1c
Explanation
I created a function that takes in a matrix and then finds the
size of that matrix and then iterates through the number of
rows and columns to and adds together the absolute values of
the elements. That total sum is then displayed.
Code
function [TotalSum]= TotalMatrixSum(Matrix)
TotalSum=0;
SizeMatrix= size(Matrix);
MaxR=SizeMatrix(1);
MaxC=SizeMatrix(2);
for IndexR = 1:(MaxR)
for IndexC = 1:(MaxC)
TotalSum=TotalSum+ abs(Matrix(IndexR, IndexC));
end
end
disp(['The sum of the absolute values of all of the matrix
elements is ' num2str(TotalSum)]);
end
Results
N/A
Problem 2
Code
function [f] = Solvef(x)
f= x^3 - 4.9*x^2 + 6.73*x - 2.011;
end
if fZero<fOne
LowBoundx= 0;
LowBoundy= fZero;
UpBoundx= 1;
UpBoundy= fOne;
else
LowBoundx= 1;
LowBoundy= fOne;
UpBoundy= 1;
UpBoundx= fOne;
end
for Index=1:4
NewX= (LowBoundx+UpBoundx)/2;
NewY= Solvef(NewX);
if NewY < 0
LowBoundx=NewX;
LowBoundy = NewY;
else
UpBoundx=NewX;
UpBoundy = NewY;
end
end
FinalMidpoint = (LowBoundx+UpBoundx)/2
Output/Command Line
FinalMidpoint =
0.4062
Results
a. I think the researcher thought a root was between 0 and 1
because the signs of the values of f(0) and f(1) are not the
same so f(x) must equal 0 at some point between x=0 and x=1.
b. The user must iterate the bisection method 5 times because
that is the i that makes (1/2)^i <.06.
c. 0.4062 using the calculations shown below and then double
checked with the code I provided.
Problem 3
Code
function [f] = Solvef(x)
f= x^3 - 5.1*x^2 + 7.02*x - 1.05;
end
if fZero<fOne
LowBoundx= 0;
LowBoundy= fZero;
UpBoundx= 1;
UpBoundy= fOne;
else
LowBoundx= 1;
LowBoundy= fOne;
UpBoundy= 1;
UpBoundx= fOne;
end
for Index=1:3
NewX= LowBoundx -
((UpBoundx-LowBoundx)*LowBoundy/(UpBoundy-LowBoundy));
NewY= Solvef(NewX);
if NewY < 0
LowBoundx=NewX;
LowBoundy = NewY;
else
UpBoundx=NewX;
UpBoundy = NewY;
end
end
NewX
Output/Command Line
NewX =
0.1735
Results
a. I think the researcher thought a root was between 0 and 1
because the signs of the values of f(0) and f(1) are not the
same so f(x) must equal 0 at some point between x=0 and x=1.
b. The code is shown above. Table is below.
Iteration LowerBound UpperBound X Approx Y of Approx
F(0) 0 -1.05
F(1) 1 1.87
1 0 1 .3596 .8614
2 0 .3596 .1975 .1454
3 0 .1975 .1735 .0197