need to make a matlab function please help! Create a function called matrixAdder
ID: 3563876 • Letter: N
Question
need to make a matlab function please help!
Create a function called matrixAdder that generates a random matrix that consists of X rows and Y columns that are normally distributed with mean M and standard deviation D, where X, Y, M, and D are inputs to your function. After generating the matrix defined above with the specific inputs, your function should calculate the sum of all numbers in the matrix and return this result back to the user. Test your function from a different script using the following set of input values and provide a formatted printout of the results. X Y M D 10 10 0 100 100 1 10 10 1000 760 -25 50 Modify your function from the previous question or write a new one to expand its functionality to also calculate the triangle's perimeter, and its area. Also, check if your triangle is an isosceles right triangle (check if two of the sides are the same length). Note that your function must return 4 things for this question: The value of c, the perimeter, the area, and 1 or 0 to indicate isosceles triangle. Provide a formatted printout of the results. Hint: Just as we can return multiple outputs from certain functions (e.g. nRows, nCols] = size(Matrix)), we can define and call our custom functions to return multiple variables using the same syntax. a. Find the 4 answers when a = 6, b = 8. b. Find the 4 answers when a = 5, b = 12. c. Find the 4 answers when a = 7, b= 24. d. Find the 4 answers when a = 10, b= 10.Explanation / Answer
Solution for 1st part:
Code for Matrix Adder: matrixAdder.m
function [result] = matrixAdder( X,Y,M,D)
rng(0,'twister');
matrix = D.*randn(X,Y) + M;
result = sum(sum(matrix));
end
Code for Script file: sol.m
testData = [10,10,0,100;
100,1,10,10;
1000,760,-25,50];
for i = 1:size(testData,1)
X =testData(i,1);
Y =testData(i,2);
M =testData(i,3);
D =testData(i,4);
fprintf('Result of matrixAdder with %d rows %d and columns with a mean of %d and standard deviation %d is : %f ',X,Y,M,D,matrixAdder(X,Y,M,D)); %This should be in one line not in two
end
Solution for Part2:
Code for function: triangleData.m
function [c, perimeter, area, isoceles] = triangleData(a,b)
c = sqrt(a^2 + b^2);
perimeter = a + b + c;
area = (a*b)/2;
if(a==b || b==c || c==a)
isoceles = 1;
else
isoceles = 0;
end
end
Code for running script: sol2.m
[c,p,a,i] = triangleData(6,8);
fprintf('c=%f, perimeter=%f, area=%f, is isoceles %d ',c,p,a,i);
[c,p,a,i] = triangleData(5,12);
fprintf('c=%f, perimeter=%f, area=%f, is isoceles %d ',c,p,a,i);
[c,p,a,i] = triangleData(7,24);
fprintf('c=%f, perimeter=%f, area=%f, is isoceles %d ',c,p,a,i);
[c,p,a,i] = triangleData(10,10);
fprintf('c=%f, perimeter=%f, area=%f, is isoceles %d ',c,p,a,i);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.