The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+25 votes
2.6k views

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$:_____. 

asked in Programming by Veteran (106k points)
edited by | 2.6k views
+1
WHY WE ARE NOT TAKING NEGATIVE VALUE i.e -1.73 becz x has two roots why we decided to take only positive value.

2 Answers

+48 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$. 

answered by Veteran (363k points)
edited by
0

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

+2
No need for getting answer, but to show that no other answer exist.
0
I didn't get if part.

How did you add 3 in 0.01 and have taken its square root? How ?

Guide me.
0
$x^2 - 3 < 0.01 \\ x^2 < 3 + 0.01$

Simple moving of 3 from LHS to RHS.
0

i didn't get for where x2 +3 /2x appears? 

0
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.
0
I like that

-(x^2-3)<0.01

Part

Nice ans
+4
@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???
+2

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.
+1

@Chhotu

Its not clear what you intend to convey , can you elaborate ?

0
Thankyou @Arjun sir for explaining so well. But why  x can't be -1.73 or -1.74?                 I didn't understand.
+4 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  x2-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

answered by Junior (731 points)
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?
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

Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,599 questions
48,599 answers
155,646 comments
63,714 users