Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
( Consider the function
f(X)=1/X-D
for which the newton-Raphson iteration gives
Xi+1=Xi-(f(Xi)/f'(Xi))=Xi-((1/Xi-D)/(-1/Xi ^2))=Xi+Xi(1-DXi)=Xi(2-DXi)
Which can be calculated from Xi using only multiplication ans subtraction , or using two fused multiply-addition. Can you expect what it does and what all care you are required to take while implementing it and can you write a C code to compute it. )

my ans:
#include<stdio.h>
main()
{
float X[15],X1[15]; //i took X for Xi and X1 for Xi+1;
int D;
printf("enter the value for X[i]:");
scanf("%f",&X[15]);
printf("\nenter the value of D:");
scanf("%d",&D);
X1[15]=X[15]*(2-(D*X[15]));
printf("X[i+1]=%f \n",X1[15]);
}

Am some what beginner at c programming.Thank you.
Posted

1 solution

Do what we all have to do when we write code: test it and see.
Compile the code, run the coffee, and work out what results you should get - Wolfram Alpha[^] may help - and then see what your code produces. If it's the same for all values you try, then it probably works.

If it doesn't...
 
Share this answer
 
Comments
Member 10791767 5-May-14 2:51am    
@originalgriff sir can you tell me is my answer is apt for the given question? because my code seems to be very small.
OriginalGriff 5-May-14 6:53am    
Have you tried it?
Member 10791767 5-May-14 10:39am    
@originalgriff. Yes i did the compilation and I am getting the output without any error. But what I did in the code is just implemented the equation(got the input as D and Xi and did the multiplications and subtraction). My code is smaller than the question. So, i doubt whether my output and code is a correct answer to the given question? Is what i did is what they are expecting?
OriginalGriff 5-May-14 10:50am    
"Compilation" != "Output"

All compilation means is that the program *can* be converted to an executable program: it doesn't mean that the program executes without error, or that it produces the correct result (or indeed any result).
So work out what input you are going to feed it, work out what result it should give, and try it...
Member 10791767 5-May-14 23:37pm    
@originalgriff I gave the input as D:1 Xi:1. so, Xi+1=1*(2-(1*1))=1.0000 is my output. Is that the output they are expecting because i cannot understand the question much clearly. If I have wrongly understood, can you help me to understand the question clearly?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900