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

MATLAB question Write a program that uses nested for loops to calculate side len

ID: 2086550 • Letter: M

Question

MATLAB question

Write a program that uses nested for loops to calculate side length c using Pythagoras' Theorem (a ^2 + b^ 2 = c^ 2 ) assuming a and b to be integer values starting from 1. Use the fprintf function to list all Pythagorean results that satisfy a >= 1, b >=1 and c <=10. A sample of the formatted results is shown below:

Pythagorean results are

a=1, b=1, c=1.41

a=1, b=2, c=2.24

a=1, b=3, c=3.16

..

a=9, b=2, c=9.22

a=9, b=3, c=9.49

a=9, b=4, c=9.85

Also use fprintf to print the number of combinations that satisfy the above restrictions and the sum of all b values of the valid combinations

Explanation / Answer

% code function [ T ] = PTriple( x ) for a = 1:x for b = 1:x for c = 1:x if c == sqrt(a^2 + b^2) fprintf ('(%d,%d,%d) ',a,b,c'); end end end end end % code