39 votes

Consider the following function.

double f(double x){ if( abs(x*x - 3) < 0.01) return x; else return f(x/2 + 1.5/x); }

Give a value $q$ (to $2$ decimals) such that $f(q)$ will return $q$:_____.

76 votes

Best answer

(We can directly go to the "if" part to get one answer, but we need to solve "else" part too to get all possible answers which though is not asked in question)

Solving the else part:

$\frac{x}{2} + \frac{3}{2x} = \frac{x^2+3}{2x}$

So, the new value of $x$ will be $\frac{x^2+3}{2x}$ and we need it equal to $x$.

$\frac{x^2+3}{2x} = x \\ \implies x^2 + 3 = 2x^2 \\ \implies x^2 = 3 \\ \implies x = 1.732 $

Now solving the if part.

abs(x*x - 3) < 0.01

So, $x^2 - 3 < 0.01 \text { and } -\left(x^2 - 3\right) < 0.01\\ \implies x^2 < 3.01 \text{ and } x^2 > 2.99\\ \implies x < 1.735 \text { and }x > 1.729$

Corrected to $2$ decimal places answer should be $1.73$ or $1.74$.

0

although the ans. can be get by the if part also, but i am not getting the else part: why (x^{2 } + 3)/2x to be equal to x ?

1

Sir,

Abs( ) returns int value so i think 1.70 is also valid and many other such values bcoz 0<0.01. So as per answer key only 1.72-1.74 but many answer are possible.

Abs( ) returns int value so i think 1.70 is also valid and many other such values bcoz 0<0.01. So as per answer key only 1.72-1.74 but many answer are possible.

5

@Arjun sir,

why have u made (x^2+3)/2x =x. ?

Its the return value of function which needs to be equal to x not the parameter itself???

why have u made (x^2+3)/2x =x. ?

Its the return value of function which needs to be equal to x not the parameter itself???

5

why have u made (x^2+3)/2x =x. ?

Because it is asked "Give a value q (to 2 decimals) such that f(q) will return q:_____. "

If this is not given then first solve IF condition to get a range of values then solve ELSE condition (using this range) to find, where does this function returns(or converges) for values of **q **(which are obtained via solving IF condition).

PS: Think about Newton–Raphson method :)

1

@chhotu .can you explain last point in some detail?how to get answer from else

if i call for f(x) and it goes to else part => and will call f(x/2 + 1.5/x) => now what to do?As per arjun sir equation we will set it to x again,so we call f(x).

f(x)=>f(x/2 + 1.5/x)=>f(x) // what is gurantee that x*x<.01 in this case? May be it satisfies if may be it not.

if i call for f(x) and it goes to else part => and will call f(x/2 + 1.5/x) => now what to do?As per arjun sir equation we will set it to x again,so we call f(x).

f(x)=>f(x/2 + 1.5/x)=>f(x) // what is gurantee that x*x<.01 in this case? May be it satisfies if may be it not.

0

0

Can you please explain why we consider else part also ???

in else part we want return **x**...in else it is return f(x/2+1.5x)

so f(x/2+1.5x) should get to ====> x...

but why x/2+1.5x ===>x

0

@Arjun sir please clear this doubt.

What will be the answer if f(q) returns q is not mentioned?

Do we equating else part with x only for this above mentioned line?

7 votes

I fell we dont need q i got q as 1.735 it is said for what of q

f(q) returns q

so if x^{2-}3 <0.01 only then f(x) would return x

so on solving x= (3.01)^{1/2 }

someone please rectify me if m wrong and make me understand

0

Your answer was right. But I didn't understand what have you done in this question? You have taken square root of 3.01. right. But how have you decided to add 3 and 0.01 and find out its answer?

I didn't get such question? Can you tell me what we have to do in this question and how did you find out its answer?

I didn't get such question? Can you tell me what we have to do in this question and how did you find out its answer?

0

actualy if you notice you will see it is a maths saying for what value of the expression x2-3 <0.01

i am saying this the maths because see in the code it is said when ever this condition is happening only then the function is returning what was given as parameter i.e f(x) =x . and they have told that in question

Give a value q(to 2 decimals) such that f(q) will return q:_____.

now to backcheck it if we square and check it absolute value the value return would be what was passed