Check what values of three variables fulfill a certain condition
2 views (last 30 days)
Hi guys! I'm stuck with a problem where if have to find all the values of x, y and z which fulfill the next equation: x^2+y^2=z^2.
I have tried it in many ways, but I can't get a result.
I don't know if you could help me..
Thank you very much!
x=[1:1:100]; % values of X
y=[1:1:100]; % values of Y
z=[1:1:100]; % values of Z
for i=1: length(x)
for l=1: length(z)
Walter Roberson on 1 Oct 2022
You do not need to loop over z. Instead, take z = sqrt(x.^2+y.^2) and test to see whether z is an integer.
And... vectorize. For a given x value you can compute for a vector of y values, and use logical indexing to figure out which (if any) entries match.
I have implemented a fully vectorized version that uses no loops at all, and I have implemented a version that uses a single loop; both approaches work fine. I am not posting the code here because you are obviously working on an assignment.