Computer Science Canada

How do you compare a bunch of different numbers from a loop?

Author:  varman [ Wed Feb 01, 2012 8:30 pm ]
Post subject:  How do you compare a bunch of different numbers from a loop?

What is it you are trying to achieve?
I need to compare a bunch of different numbers from a loop in order to show which gave the smallest output of an equation.


What is the problem you are having?
I don't know how to compare the numbers. Basically, the question asks to compute values of the function f(x) = 3x2 ? 2x+1 in steps of 0.1 between x=0 and x=1 inclusive and find the
value of x for which f(x) is a minimum. So I am having trouble with the last part.


Describe what you have tried to solve this problem
I tried to think of a solution but couldn't.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<var fx : real := 0.0
var ans : real
loop
    fx := fx + 0.1
    ans := 3 * (fx ** 2) - 2 * fx + 1
    put " When x is ", fx, " the answer is ", ans
    exit when fx > 0.9
end loop
if fx < fx then
put "The answer was the smallest when f(x) was "
end if
>



Please specify what version of Turing you are using
4.1

Author:  Dreadnought [ Wed Feb 01, 2012 9:36 pm ]
Post subject:  Re: How do you compare a bunch of different numbers from a loop?

Perhaps you could keep track of the minimum value as you iterate over your input. Then for each input, you can compare the value of the function to the minimum.

Author:  varman [ Wed Feb 01, 2012 10:23 pm ]
Post subject:  RE:How do you compare a bunch of different numbers from a loop?

But there is no input. Everything happens without the user typing in anything

Author:  DemonWasp [ Wed Feb 01, 2012 10:48 pm ]
Post subject:  RE:How do you compare a bunch of different numbers from a loop?

So...keep track of the minimum value as you loop over the values of x.

Author:  varman [ Wed Feb 01, 2012 11:26 pm ]
Post subject:  RE:How do you compare a bunch of different numbers from a loop?

How do I do that? Is there a variable automatically assigned to every value of x?

Author:  DemonWasp [ Thu Feb 02, 2012 9:12 am ]
Post subject:  RE:How do you compare a bunch of different numbers from a loop?

Well, right now you have the value of x (which increases by 0.1 and so on) in a variable called 'fx' (not sure why). You have the value of f(x), which I would ordinarily call 'f', as 'ans'.

Author:  Velocity [ Tue Feb 07, 2012 5:34 pm ]
Post subject:  RE:How do you compare a bunch of different numbers from a loop?

so at the beginning of the loop state the new fx value so that it tracks it ever time it executes the loop


: