1 -2 0 0 0 1 -2 0 3. Create the following matrix l0 0 0 a Pre-allocate by settin
ID: 3813589 • Letter: 1
Question
1 -2 0 0 0 1 -2 0 3. Create the following matrix l0 0 0 a Pre-allocate by setting all elements to zero. Use two sequential for-loops and no if-statements. b) Pre-allocate by setting all elements to zero. Use two nested for-loops with an if-elseif-statement. c) Pre-allocate by setting all diagonal elements to one. Use a while-loop with no if-statements. 4. The body surface area (BSA) of a person is given by the formula: BSA 0.007 184W us 0,75 Where W is mass in kg and His height in cm. Check your results for a 95-kg, 1.87-m-person. a Write a function file BodySurA(w,h) to compute BSA. Call the function from your script file with input prompts for W and H. b) In your script file, write an anonympous function to determine BSA.Explanation / Answer
Code for question 3
disp('original matrix');
a = randi(31, 5, 5)
for i=1:5
for j = 1:i
a(i,j) = 0; %lower triangular matrix to zero
end
end
disp('after first iteration');
a
for i=1:5
for j = 1:5
if i<j-1
a(i,j) = 0; %upper corner to zero
elseif i+1 == j
a(i,j) = -2; % set required items to -2
end
end
end
disp('after second iteration');
a
i = 1;
while i <= 5 %single while loop to update diagonals
a(i, i) = 1; %update diagonals
i = i + 1;
end
disp('after third iteration');
a
output:
original matrix
a =
20 19 4 5 16
8 14 9 9 14
15 23 10 5 5
6 12 22 1 14
6 7 6 20 24
after first iteration
a =
0 19 4 5 16
0 0 9 9 14
0 0 0 5 5
0 0 0 0 14
0 0 0 0 0
after second iteration
a =
0 -2 0 0 0
0 0 -2 0 0
0 0 0 -2 0
0 0 0 0 -2
0 0 0 0 0
after third iteration
a =
1 -2 0 0 0
0 1 -2 0 0
0 0 1 -2 0
0 0 0 1 -2
0 0 0 0 1
4.
%this one is using anonymous function
W = input('Enter weight: '); %input weight from user
H = input('Enter height: '); %input height from uset
BodySurA = @(w,h) 0.007184 * w^0.425 *h^.75; %declare anonymous function
printf('BSA is %d ',BodySurA(W,H)); %call anonymous function inside print function
---------------------------------------------------------------------------------
%using general function notation
function BSA = BodySurA(w,h) %declare standard function
BSA = 0.007184 * w^0.425 *h^.75; %calculate bsa
end
W = input('Enter weight: ');
H = input('Enter height: ');
printf('BSA is %d ',BodySurA(W,H));
--output--
Enter weight: 95
Enter height: 1.87
BSA is 0.0795756
I coded the program in a very simple but systematic way. Let me know through the comments if you face any difficulty understanding the code. I shall be glad to help you with this problem.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.