Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

EE 221 Computing for Engineers Homework #7 Problem #1 The sum of the 1-D moments

ID: 2248794 • Letter: E

Question

EE 221 Computing for Engineers Homework #7 Problem #1 The sum of the 1-D moments using a for loop In statics, the sum of moments applied on an object must be zero. Let the ith moment be defined as: Where Fi is the ith force, di is the distance of Fi from the center of gravity and m the ith moment caused by Fi. For the body to be static (not rotating), the sum of the moments must be zero which is expressed as: Write a program in MATLAB that will take the number of forces as input. Then it will go through a for loop for each force. Within the for loop, Matlab will ask the user to enter the value of the forceFi and the distance from the center of gravity d. Then it will iteratively calculate it corresponding moment m and then Matlab will add it to the sum_moment. Once the for loop is exited, display the sum using disp command. Submit the script as .m file HINT: Use the for loop example in class, that summed the forces as a guideline. Now you need to add the distance and calculate m, before adding to to sum_moment. Problem #2 while loop to search for the maximum value In MATLAB, create a random vector R rand(10,1). Using a while loop, search for the maximum value within the vector using if-else statement.. You should have the maximum value to be stored in a variable that is called Max_Value. Submit the script as .m file Hint: Initialize the maximum value to be the first element in the vector. Then compare the current maximum value to the next element in the vector. Exit once the loop searched all the values. Problem #3 Get the average value of 2D matrix using nested for loop In MATLAB, create a random matrix M rand(5,6). Use a nested for loop to get the average value of the matrix. Hint Initialize a variable called sum-0 to zero before the loop. Then, Iteratively add the value of each element in the matrix to sum. Once all the elements are added, the average ssum/(5x6).

Explanation / Answer

Matlab Script:

clc;
clear all;
%question 1
n = input('Enter the number of forces: ');
sum_moment = 0;
for i=1:n
Fi = input('Enter the force Fi: ');
di = input('Distance from gravity di: ');
sum_moment = sum_moment +(Fi*di);
end
fprintf('Sum moment: %d ',sum_moment);

%question 2
R = rand(10,1);
max_value = R(1);
i = 2;
while i<=length(R)
if R(i)>max_value%checks for maximum value
max_value = R(i);
end
i = i+1;
end
fprintf('Maximum value: %d ',max_value);

%question 3
M = rand(5,6);
sum1 = 0;

%nested loop
for i=1:5
for j=1:6
sum1 = sum1+M(i,j);
end
end
the_average = sum1/(5*6);
fprintf('Average of M: %d ',the_average);

command window output:

Enter the number of forces:
3
Enter the force Fi:
2
Distance from gravity di:
2
Enter the force Fi:
5
Distance from gravity di:
5
Enter the force Fi:
8
Distance from gravity di:
6
Sum moment: 77
Maximum value: 7.951999e-001
Average of M: 4.874437e-001
>>