Using for loops to find values of n

4 views (last 30 days)
Bob Snow
Bob Snow on 24 Jan 2017
Edited: Stephen23 on 24 Jan 2017
Hi I wrote this code to try to find 3 to which integer powers (n) is equal to the formula s^2-3240. Where s is from 57 to 100.
for s=57:100
for n=1:100
r(s)=log(s^2-3240)/log(3);
if r==n
disp(n)
end
end
s=s+1;
end
However, nothing is showing up in my command window. If I double click on r in my workspace I see that at s=57 r=2 so it should display 2 as an answer since within the for-loop n=2 at one point but nothing comes up and there are other times that r is equal to an integer (n) when s is greater than 57 but nothing comes up in the command window.

Answers (2)

Karsten Reuß
Karsten Reuß on 24 Jan 2017
Edited: Karsten Reuß on 24 Jan 2017
1. your r is never eqal to n, because n is a single number and r is a vector.
2. your code only works when you put round(r(s))==n as a test, because the result you get for 57 is not exactly equal to 2 (just MATLAB doesn't display in this precision).
Like this it gives you 3 numbers (2,6,8) as the result:
for s=57:100
for n=1:100
r(s)=log(s^2-3240)/log(3);
if abs(r(s)-n)<0.0001
disp(n)
end
end
s=s+1;
end

Stephen23
Stephen23 on 24 Jan 2017
Edited: Stephen23 on 24 Jan 2017
Welcome to the world of floating point numbers. Many beginners get confused by floating point numbers, because they imagine that a computer calculates things just like in mathematics. That a simple operation like
log(3)
yields one perfect, "true" value. That this "true" value can be operated on and compared with other "true" values. That the "true" value can be stored numerically in their computer.
The reality is that numeric calculations have a limited precision. Think about that for a moment: your computer cannot calculate log(3) to infinite precision. It calculates it to the precision of the data type that is used: double is the default type and it has around sixteen digits of precision. This means that if you are relying on numeric calculations to provide you with infinite precision (and you are), then you will be disappointed.
In any case, this is a topic that has been discussed a thousand times before on this forum, so get reading if you want to take the opportunity and learn something new today:
This is worth reading as well:
PS: the solution, as every of those links will tell you, is never compare for equality between floating point values, and always use a tolerance:
abs(r-n) < tol
PPS: You do not need to increment the s value yourself. The for loop does this for you. That is what for loops do.
PPPS: also the if comparison does not do what you think it does: when providing a vector or array then all elements much be true for that if to run, which clearly in your case will never happen. Try running this:
for s=57:100
for n=1:100
r = log(s^2-3240)/log(3);
if abs(r-n)<1e-3
disp([s,n])
end
end
end
which displays this:
57 2
63 6
99 8
Keep playing around, and check that it really does what you think it should be doing...

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!