#include <iostream>
#include <cmath>

double f1(double);
double f2(double,double);

using namespace std;

int main()
{
	double dx, dy,x=1.0, y=1.0,xnew,ynew,eps=0.0000001,err=1.0,m=1;
	while ( fabs(err) > eps )
	{
    dx= -f1(x)/(3.0*sin(x)+cos(x));
	dy= (f1(x)*sin(x)+f2(x,y)*(cos(x)-3*sin(x)))/3*sin(x)*(cos(y)+cos(x)*cos(y));
	
	xnew= x+dx;
	ynew= y+dy;

	m = (xnew*xnew)+(ynew*ynew);
	err=sqrt(m);
	}
	if ( fabs(err) < eps )
		cout<< "dx= " <<dx<<endl;
	    cout<< "dy= "<<dy<<endl;
	
return 0;
}

double f1(double x)
{
	return sin(x)+3.0*cos(x)-2.0;
}

double f2(double x, double y)
{
	return cos(x)-sin(y)+0.2;
}

Hi guys..This is the question; find the solution of sin x+3cos x -2=0 and cosx-siny +0.2=0 in the vicinity of the point (1,1).
There is no error when I debug but nothing come out in the window. Can somebody please help me

Recommended Answers

All 8 Replies

You got the code tags backwards, put them around your code and don't put them around your question. There's still time to edit.

You got the code tags backwards, put them around your code and don't put them around your question. There's still time to edit.

Thank you for correcting me.

Double check your error formula. The error gets stuck at 1.419 or thereabouts. It could be that your chosen points are too far away from the solution but I didn't do any further calculations. Widen your epsilon a bit so you can debug without the program going on forever and then bring it down gradually to its current level.

EDIT: So I think one of the problems is that you never assign xnew back to x and ynew back to y. However, when I do that the errors grow into the 50000 range. I looked over some of the material online about the algorithm (I'm afraid I don't have much experience with it) and everything seemed to point to a negative correction term proportional to the slope of the derivative. Again, look over your material and see what you come up with.

OK first up, I'm not familiar with Newton Raphson so I can't actually help you out with the algo. But I can see the problem with your code.

The reason you never see any output is because your while loop loops infinitely because the value of the variable 'err' never changes.

None of the values ever change after the first time through the loop:
'x' and 'y' stay as 1.0 and 1.0.
So the calculated values of 'dx' and 'dy' are the same on each iteration of the loop.
Therefore 'xnew' and 'ynew' also have the same value calculated each time, as do 'm' and 'err'.

It's obvious to me that you're trying to loop until the potential error in the calculated value has dropped below a certain threshold, in order to get an accurate result.

The only way you're going to be able to decrease the amount of error would be to mutate one or more of the variables, based on one or more of the other calculated variables. Don't ask me which ones though, I really don't know. I'd leave that to one of the more educated CS types here!

My uneducated guess would be to assign x the value of xnew and y the value of ynew before reiterating, but I could be drastically wrong on that! I haven't got time to look up Newton Raphson either..Sorry!

Otherwise, perhaps there is something else that is missing from your implementation.

Either way, you'll need to do something to modify one or more values, which should then cause different values to be calculated each time through the loop until the error drops below the error threshold....But as I've said, I'll leave that to you to work out!

Also, one other minor thing. You don't need the if statement at line 23, it is redundant. By the time you've got to that point of the program, the value of fabs(err) will inevitably be less than eps as your while loop only ends when this is the case.
Cheers for now,
Jas.

guys...thank you so much for the reply...everything still confuse me...but i came out with new code.

#include <iostream>
#include <cmath>

double f1(double);
double f2(double,double);

using namespace std;

int main()
{
	double dx, dy,x1=1.0,y1=1.0,eps=0.0001,err=1.0,m=1.0;

	do
	{
	dx= -f1(x1)/(3.0*sin(x1)+cos(x1));
	dy= (f1(x1)*sin(x1)+f2(x1,y1)*(cos(x1)-3*sin(x1)))/3*sin(x1)*(cos(y1)+cos(x1)*cos(y1));
	x1=x1+dx;
	y1=y1+dy;
	m = (x1*x1)+(y1*y1);
	err=sqrt(m);
	}
	while ( fabs(err) > eps);
	{
	x1=x1+dx;
	y1=y1+dy;
	dx= -f1(x1)/(3.0*sin(x1)+cos(x1));
	dy= (f1(x1)*sin(x1)+f2(x1,y1)*(cos(x1)-3*sin(x1)))/3*sin(x1)*(cos(y1)+cos(x1)*cos(y1));
	
	}
	if ( fabs(err) < eps )
		cout<< "dx= "<<x1<<endl;
	    cout<< "dy= "<<y1<<endl;
	
return 0;
}

double f1(double x)
{
	return sin(x)+3.0*cos(x)-2.0;
}

double f2(double x, double y)
{
	return cos(x)-sin(y)+0.2;
}

Problem still the same.nothing come out..

The err calculation (line 19) is meaningless. You need to watch not x^2+y^2 as you do, but f1^2 + f2^2. The goal is to get f1 and f2 to approach 0, isn't it?
The dy calculation (line 16) is wrong. How did you come up with this formula?
What is the purpose of lines 23-29? They would be executed once, after the loop has finished, that is the required accuracy already achieved.

The err calculation (line 19) is meaningless. You need to watch not x^2+y^2 as you do, but f1^2 + f2^2. The goal is to get f1 and f2 to approach 0, isn't it?
The dy calculation (line 16) is wrong. How did you come up with this formula?
What is the purpose of lines 23-29? They would be executed once, after the loop has finished, that is the required accuracy already achieved.

Hi...I appreciate you helping me.Why don't you check out this link and I think I got the algorithm right. dy is not the differentiation of y but dy is delta y.

First of all,

(f1(x1)*sin(x1)+f2(x1,y1)*(cos(x1)-3*sin(x1)))/3*sin(x1)*(cos(y1)+cos(x1)*cos(y1))

is
((f1(x1)*sin(x1)+f2(x1,y1)*(cos(x1)-3*sin(x1)))/3)*sin(x1)*(cos(y1)+cos(x1)*cos(y1))
I don't think it is your intention.

Second, even assuming correct parenthesis placement, I can't see how you came up with this. I am getting something very different. Again, if you show step by step your math, that would clarify things a lot.

On a side note, I'd recommend to keep the algorithm related code as generic as possible. What I mean is that you factored functions out of the algorithm, yet hardcoded derivatives. Try to implement the algorithm in terms of functions and derivatives, and have derivatives calculated in separate routines. You will see how much more clean and manageable the code will become.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.