Understanding Newton's Method in C++

Click For Summary

Discussion Overview

The discussion revolves around a C++ implementation of Newton's Method for finding roots of a function. Participants are addressing issues related to the loop not executing as expected, examining the mathematical functions defined in the code, and suggesting modifications to improve the algorithm's performance.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions why the loop is not functioning as intended and suggests tracing the values of variable 'a' and the outputs of functions fun1 and fun2.
  • Another participant challenges the condition used in the while loop, asking if it is appropriate.
  • A suggestion is made to use two variables for iterations and to stop the loop when the difference between them is less than 0.001.
  • Concerns are raised about the mathematical correctness of function fun2, with one participant asserting that it appears correct while another participant acknowledges a misreading of the original function.
  • There is a discussion about a potential error in the equation provided in the homework statement, specifically regarding the term 4^2, which should involve multiplication with x.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the mathematical functions and the loop's condition. There is no consensus on the best approach to resolve the issues presented.

Contextual Notes

Participants note potential misunderstandings regarding the mathematical definitions and the implementation of the algorithm, but do not resolve these issues definitively.

Who May Find This Useful

Students learning about numerical methods, particularly Newton's Method, and those interested in debugging C++ code related to mathematical functions.

Sumaya
Messages
29
Reaction score
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
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?
 
Code:
while(fun2(a)<0.001);

Are you sure about this?
 
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);
 
Also, check your (math in) function FUN2.
 
TheoMcCloskey said:
Also, check your (math in) function FUN2.
I don't see anything wrong with it.
 
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.
 
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.
 
thank u all
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K