Understanding Newton's Method in C++

In summary, the conversation discusses a code for a loop that is not working properly and requests for help in fixing it. The code uses two functions, fun1 and fun2, and the goal is to find the root of fun1 by using the Newton-Raphson method. Some suggestions are given, such as checking the values of the variables and the functions, as well as using two variables for iterations and checking the difference between them to determine when to stop the loop.
  • #1
Sumaya
29
0

Homework Statement



why the loop not looping ?

Homework Equations



fun1=x^3+4^2-x-1
fun2=3x^2+8x-1

The Attempt at a Solution



Code:
#include "stdafx.h"
#include<iostream>
using namespace std;
float fun1(float);
float fun2(float);
void main()
{
float a;
cin>>a;

if (fun2(a)>0)

do
{
	a=a-(fun1(a)/fun2(a));
	cout<<a<<endl;

	
}
	while(fun2(a)<0.001);
	


}
		 
float fun1( float a)
{
	float y;
	y=a*a*a+4*a*a-a-1;
	return y;
}
float fun2( float b)
{
	float y;
	y=3*b*b+8*b-1;
	return y;
}
 
Physics news on Phys.org
  • #2
Try to trace your program watching values of variable a and values returned by fun1 and fun2. Is it doing what you hoped for, or is it doing something else?
 
  • #3
Code:
while(fun2(a)<0.001);

Are you sure about this?
 
  • #4
Keep two variables for iterations.
If the difference between them is < 0.001 then stop loop.

Something like this :

x1 = a - (fun1(a) / fun2(a));

do{
x0 = x1;
x1 = x0 - (fun1(x0) / fun2(x0));
d = x0 - x1;
}while(d > 0.001);
 
  • #5
Also, check your (math in) function FUN2.
 
  • #6
TheoMcCloskey said:
Also, check your (math in) function FUN2.
I don't see anything wrong with it.
 
  • #7
I don't see anything wrong with it.

You're CORRECT - I misread OP's function F(x)! fun2 code is correct for fun1 code.
 
  • #8
TheoMcCloskey said:
You're CORRECT - I misread OP's function F(x)! fun2 code is correct for fun1 code.
?

Maybe you're referring to this line in the OP, in the Relevant equations section:
sumaya said:
fun1=x^3+4^2-x-1
There's a missing x. 4^2 should be 4*x^2.
The code has the right formula, though.
 
  • #9
thank u all
 

Related to Understanding Newton's Method in C++

1. What is Newton's Method and why is it used in C++?

Newton's Method is an iterative method used to find the root of a given function. It is commonly used in C++ because it is a powerful and efficient way to solve equations and find accurate solutions.

2. How does Newton's Method work in C++?

In C++, Newton's Method works by using an initial guess and repeatedly applying a formula to refine the guess until it converges to the root of the function. This process is repeated until a desired level of accuracy is achieved.

3. What are the advantages of using Newton's Method in C++?

One of the main advantages of using Newton's Method in C++ is its speed and efficiency. It is also a versatile method that can be used to solve a wide range of mathematical problems, making it a valuable tool for scientists and engineers.

4. Are there any limitations to using Newton's Method in C++?

Yes, there are some limitations to using Newton's Method in C++. One of the major limitations is that it may fail to converge if the initial guess is not close enough to the root or if the function has multiple roots. It also requires the function to be differentiable.

5. Can Newton's Method be used to solve real-world problems?

Yes, Newton's Method can be used to solve real-world problems in various fields such as physics, engineering, and economics. It is particularly useful in situations where a system can be modeled by a mathematical function and a solution needs to be found. However, it is important to carefully consider the limitations and accuracy of the method when applying it to real-world problems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
774
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
886
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top