in Programming in C edited by
18,115 views
59 votes
59 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$:_____. 

in Programming in C edited by
18.1k views

4 Comments

@Shaik Masthan sir

$x^2$ < 3.01  ====> x < 1.735 or x >  -1.735

isn’t this must be and?  because if it is or then it covers whole real line.

although in 2nd or is ok.

1
1

@neel19 nhi bhai,baat sirf f(q) q return krega aisi hai

q negative ho sakta hai

1
1

4 Answers

97 votes
97 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$. 

edited by
by

28 Comments

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 ?

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

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

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

Simple moving of 3 from LHS to RHS.
0
0

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

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

-(x^2-3)<0.01

Part

Nice ans
0
0
@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???
7
7
edited by

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 :)

8
8
edited by
@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
1

@Chhotu

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

1
1
Thankyou @Arjun sir for explaining so well. But why  x can't be -1.73 or -1.74?                 I didn't understand.
1
1
edited by

@Arjun sir

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
0

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

there is + operator but not comma 

0
0
and $f(q)$ must return $q$ and not $-q$
3
3

and f(q) must return q and not −q

why ?

0
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?

0
0
The correct answer actually should be 1.73, but not a range since the question specifically says to give 'a' value to two decimal places. Only possible answer in this case is 1.73 as (1.74)^2 > 3.01 and abs(1.72^2-3)>0.01

1.74^2=3.027

1.72^2=2.958
0
0
why the answer is 1.74??
0
0
Can you please explain a little more. I am a little confused about this.
0
0

Which section you’re having issue with @neel19?

0
0
Why the answer should not be all the numbers between the range (1.42-1.99) because every number is going to return the value same as we pass in this range,

Let's take x=1.42

  Now, abs(1.42*1.42-3)= abs(2.0164-3)=0

If(0<0.01) it's true so it'll return 1.42
0
0

@gatecse The value of $x > 1.729 \text { and }  x < 1.735$. Thus the answer must be $1.73$ as we need to give a value (to $2$ decimals). The answer can’t be $1.74$ as it exceeds the range.

1
1
That's correct when we do precisely. But if we round off (round up), 1.735 to 2 decimal points we get 1.74.
1
1
edited by

@Abhrajyoti00

The range of x


[1.729 < x < 1.735 or
 -1.735 <x <-1.729]


So, answer can be 1.73 or -1.73. but answer can’t be 1.74

I think x can be negative also.

plz explain.

1
1

@Argharupa Adhikary Yes you are correct. Both $-1.73$ and $1.73$ satisfy the program. Hence ans should be $-1.73$ or $1.73$ (in to $2$ decimals)

@gatecse Sir pls look at this.

0
0

Code :  The code is in python, since C was giving TLE (prob due to formatting issues).

I have attached the outputs here in form of tuple $(x, f(x))$. The bold values are the ones where f(x) was actually giving x as output.

(1.726, 1.7320614136732329)
(1.728, 1.7320555555555557)
(1.73, 1.73)
(1.732, 1.732)
(1.734, 1.734)
(1.736, 1.7320552995391705)
(1.738, 1.7320609896432682)
(1.74, 1.7320689655172414)
(-1.74, -1.7320689655172414)
(-1.738, -1.7320609896432682)
(-1.736, -1.7320552995391705)
(-1.734, -1.734)
(-1.732, -1.732)
(-1.73, -1.73)
(-1.728, -1.7320555555555557)

 

We can notice both $1.73$ and $-1.73$ gives correct answer. While $1.74$ or $-1.74$ does not give desired output.

@gatecse Sir, @ankitgupta.1729 Sir.

2
2

THANKS FOR EXPLAINING @Abhrajyoti00

2
2
9 votes
9 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

by

2 Comments

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
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

1
1
0 votes
0 votes
0 votes
0 votes
Step 1: abs() return positive value , eg abs(-2)=2;

If abs(x*x-3) return a value which is less than 0.01, then it condition satisfy and return x;

So, x*x-3<0.01 => x<1.732

Step 2: Recheck f(1.73) will return 1.73 or not?
Answer:

Related questions