Im trying to write a golden section optimization search program. I have written it to this point and im getting 25 errors in it and i do not know how to fix them. I am new to programming and did do research on the errors and am still unsure.

I still have to add a few more steps to the end including a way to print the results at the end.

any help is greatly appreciated.

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define a 1.61803;


//Function to be analyzed
double F(double x) {
  return x*x-20*x+100;
	}

double xi, xu, xmax, fu, fi, f1, x1, f2, x2;

xi=0;
xu=100;
xmax=1000;
fu=F(xu); 
fi=F(xu);

{	do {
		int	x1=xu;
		int	f1=fu;
		xu=x1*(1+a)-xi*a;
		if {
			(xu>xmax);
				break;
		}
		fu=F(xu);
		if {
			(fu>f1);
			return 0;
		}
		else {
			xi=x1;
			fi=f1;
		}
		
	} while (fu>fi);
	return 0;
}

double N=13;
double t=.381966;

int x1=(1-t)*xi+t*xu;
int f1=F(x1);
int x2=t*xi+(1-t)*xu;
int f2=F(x2);
double k=3;
E:
k = k+1;
if {
	(k>N);
	return 0;
}
else {
	if {
		(f1>f2);
		xi=x1;
		fi=f1;
		x1=x2;
		f1=f2;
		x2=t*xi+(1-t)*xu;
		f2=F(x2);
		goto E;
	}
	else {
		xu=x2;
		fu=f2;
		x2=x1;
		f2=f1;
		x1=(1-t)*xi+t*xu;
		f1=F(x1);
		goto E;
	}
}

}

Recommended Answers

All 21 Replies

All the code beginning on line 13 is not inside any function. The function started on line 9 ends on line 11, and you did not define any other function name in any of the remaining lines of code.

does this make any better sense Ancient Dragon?

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define a 1.61803;


//Function to be analyzed
double F(double x) {
return x*x-20*x+100;
}
void Golden(double ix, double xu, double xmax, double fu, 
double fi, double x1, double f2, double x2) {


xi=0;
xu=100;
xmax=1000;
fu=F(xu); 
fi=F(xu);

{ do {
x1=xu;
f1=fu;
xu=x1*(1+a)-xi*a;
if {
(xu>xmax);
break;
}
fu=F(xu);
if {
(fu>f1);
return 0;
}
else {
xi=x1;
fi=f1;
}

} while (fu>fi);
return 0;
}

double N=13;
double t=.381966;

int x1=(1-t)*xi+t*xu;
int f1=F(x1);
int x2=t*xi+(1-t)*xu;
int f2=F(x2);
double k=3;
E:
k = k+1;
if {
(k>N);
return 0;
}
else {
if {
(f1>f2);
xi=x1;
fi=f1;
x1=x2;
f1=f2;
x2=t*xi+(1-t)*xu;
f2=F(x2);
goto E;
}
else {
xu=x2;
fu=f2;
x2=x1;
f2=f1;
x1=(1-t)*xi+t*xu;
f1=F(x1);
goto E;
}
}

}

Thread: golden section program problems
Forum: C++

Where is the main program?

do I need a main() for the program to solve the function?

I was thinking of taking values out and defining them in a main function after the golden() function is that a better route to take?

For every C++ program, you need a starting point, and that starting point
should be either "int main()" or "int main(char *arg, char *argv[])"

Here is a complete C++ program :

#include <iostream>
using namespace std;

int zero(){
   return 0;
}
int main()
{
  int x = zero();
  
   cout << x << endl;

    return 0;
}

You still have that block from 22-38 that's segregated without any reason for doing so. You're returning 0 from a function that has void type (so it shouldn't return anything) and you have a dreaded goto in your code which should only possibly be used under the most dire of circumstances.
Take a look at some tutorials on functions on the web,
e.g., http://www.cplusplus.com/doc/tutorial/functions/
I think you are missing some critical pieces of the language that you need to proceed.

Once you've gotten to refining your program a bit take a look at this:
http://pagesperso-orange.fr/jean-pierre.moreau/Cplus/golden_cpp.txt
(it uses void main which is incorrect, main should always return an int; however the rest of the code is solid and it's broken up into functions much like you are aiming to do).

I checked out the two sites and this site http://pagesperso-orange.fr/jean-pie...golden_cpp.txt does the same thing i am trying to do however it follows a slightly different method that was assigned to me.

the following code gives me less errors but i still don't understand the errors i get.

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define a 1.61803;


//Function to be analyzed
double F(double x) {
  return x*x-20*x+100;
	}
void Golden(double xi, double xu, double xmax, double *fu, 
			double *fi, double *x1, double *f1, double *f2, double *x2)  {


xmax=1000;
fu=F(xu); 
fi=F(xu);

	do {
		x1=xu;
		f1=fu;
		xu=x1*(1+a)-xi*a;
		if (xu>xmax)
		{
			break;
		}
		fu=F(xu);
		if (fu>f1){
			
				}
		else {
			xi=x1;
			fi=f1;
		}
		
	} while (fu>fi);

double N=13;
double t=.381966;

x1=(1-t)*xi+t*xu;
f1=F(x1);
x2=t*xi+(1-t)*xu;
f2=F(x2);
double k=3;
E:
k = k+1;
if (k>N)
{
	
}
else {
	if (f1>f2)
	{
		xi=x1;
		fi=f1;
		x1=x2;
		f1=f2;
		x2=t*xi+(1-t)*xu;
		f2=F(x2);
		goto E;
	}
	else {
		xu=x2;
		fu=f2;
		x2=x1;
		f2=f1;
		x1=(1-t)*xi+t*xu;
		f1=F(x1);
		goto E;
	}
}
void main() {

  ax=0; bx=100;

  golden(ax,bx,&x3,&f1,&f2,&f3);

  printf("\n Function minimum is %f\n\n",f1);
  printf(" for X= %f\n\n",x1);


}

I chose to use the goto loop to setup the k counter. is there a better way to setup a counter i basically want the program to iterate 13 times before it goes to the next step.

I chose to use the goto loop to setup the k counter. is there a better way to setup a counter i basically want the program to iterate 13 times before it goes to the next step.

Yes, thats what loops are there for, to iterate certain amount of times.

#define a 1.61803 /* no semicolon */

Your function before main is missing a closing brace.

Most of the errors show that you might not be aware of how to use pointers?

yeah you are right I am not sure how to use them i tried to follow an example. I will do more research on pointers.

thanks

I got the code down to two errors
error C2601: 'main' : local function definitions are illegal
(83) : fatal error C1075: end of file found before the left brace '{' at

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define a 1.61803


//Function to be analyzed
double F(double x) {
  return x*x-20*x+100;
	}
void Golden(double xi, double xu, double xmax, double *fu, 
			double *fi, double *x1, double *f1, double *f2, double *x2)  {


xmax=1000;
*fu=F(xu); 
*fi=F(xu);

	do {
		*x1=xu;
		f1=fu;
		xu=*x1+*x1*a-xi*a;
		if (xu>xmax)
		{
			break;
		}
		*fu=F(xu);
		if (fu>f1){
			
				}
		else {
			xi=*x1;
			fi=f1;
		}
		
	} while (fu>fi);

double N=13;
double t=.381966;

*x1=(1-t)*xi+t*xu;
*f1=F(*x1);
*x2=t*xi+(1-t)*xu;
*f2=F(*x2);
double k=3;
if (k>N)
{
	
}
else {
	if (f1>f2)
	{
		k=k+1;
		xi=*x1;
		fi=f1;
		x1=x2;
		f1=f2;
		*x2=t*xi+(1-t)*xu;
		*f2=F(*x2);
	}
	else {
		k=k+1;
		xu=*x2;
		fu=f2;
		x2=x1;
		f2=f1;
		*x1=(1-t)*xi+t*xu;
		*f1=F(*x1);
	}
}
int main() {

  ax=0; bx=100;

  golden(ax,bx,&x3,&f1,&f2,&f3);

  printf("\n Function minimum is %f\n\n",f1);
  printf(" for X= %f\n\n",x1);


}

You need one more } between lines 71 and 72 (there's a mismatch in your braces). Also your function name must match with respect to case, so make it golden or Golden in both places.

Also, since this is a C++ program (sitting square on the fence actually it could practically be either except for your namespace statement) you should probably include <cstdio> and <cmath> rather than stdio.h and math.h.

ok one more error I searched online and I still don't know what I did wrong.

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define a 1.61803


//Function to be analyzed
double F(double x) {
  return x*x-20*x+100;
	}
void Golden(double xi, double xu, double xmax, double *fu, 
			double *fi, double *x1, double *f1, double *f2, double *x2)  {


xmax=1000;
*fu=F(xu); 
*fi=F(xu);

	do {
		*x1=xu;
		f1=fu;
		xu=*x1+*x1*a-xi*a;
		if (xu>xmax)
		{
			break;
		}
		*fu=F(xu);
		if (fu>f1){
			
				}
		else {
			xi=*x1;
			fi=f1;
		}
		
	} while (fu>fi);

double N=13;
double t=.381966;

*x1=(1-t)*xi+t*xu;
*f1=F(*x1);
*x2=t*xi+(1-t)*xu;
*f2=F(*x2);
double k=3;
if (k>N)
{
	
}
else {
	if (f1>f2)
	{
		k=k+1;
		xi=*x1;
		fi=f1;
		x1=x2;
		f1=f2;
		*x2=t*xi+(1-t)*xu;
		*f2=F(*x2);
	}
	else {
		k=k+1;
		xu=*x2;
		fu=f2;
		x2=x1;
		f2=f1;
		*x1=(1-t)*xi+t*xu;
		*f1=F(*x1);
	}
}
}
int main() {

	int x3, f1, f2, f3, x1;
  	int ax=0; 
	int bx=100;

  golden(ax,bx,&x3,&f1,&f2,&f3);

  printf("\n Function minimum is %f\n\n",f1);
  printf(" for X= %f\n\n",x1);

}

I got it to run but I get the wrong answer I should get a x of 10.

thanks for all the quick responses.

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define a 1.61803


//Function to be analyzed
double F(double x) {
  return x*x-20*x+100;
	}
void Golden(double xi, double xu, double xmax, double *fu, 
			double *fi, double *x1, double *f1, double *f2, double *x2)  {


xmax=1000;
*fu=F(xu); 
*fi=F(xu);

	do {
		*x1=xu;
		f1=fu;
		xu=*x1+*x1*a-xi*a;
		if (xu>xmax)
		{
			break;
		}
		*fu=F(xu);
		if (fu>f1){
			
				}
		else {
			xi=*x1;
			fi=f1;
		}
		
	} while (fu>fi);

double N=13;
double t=.381966;

*x1=(1-t)*xi+t*xu;
*f1=F(*x1);
*x2=t*xi+(1-t)*xu;
*f2=F(*x2);
double k=3;
if (k>N)
{
	
}
else {
	if (f1>f2)
	{
		k=k+1;
		xi=*x1;
		fi=f1;
		x1=x2;
		f1=f2;
		*x2=t*xi+(1-t)*xu;
		*f2=F(*x2);
	}
	else {
		k=k+1;
		xu=*x2;
		fu=f2;
		x2=x1;
		f2=f1;
		*x1=(1-t)*xi+t*xu;
		*f1=F(*x1);
	}
}
}
int main() {

	double x3, f1, f2, f3, x1, fu, fi, x2;
  	int ax=0; 
	int bx=100;
	x3=1000;

  Golden(ax,bx,x3,&fu,&fi,&x1,&f1,&f2,&x2);

  printf("\n Function minimum is %f\n\n",f2);
  printf(" for X= %f\n\n",x1);

}

[Oops. I'm out of sync. (Was replying to #13 before I saw the other replies.)]

For me personally I would need some comments at least at the major steps (or if you have the pseudocode somewhere) to figure out your logic error.

I don't think this is the cause of the error at all but one thing that you should address is this section

double N=13;
double t=.381966;

*x1=(1-t)*xi+t*xu;
*f1=F(*x1);
*x2=t*xi+(1-t)*xu;
*f2=F(*x2);
double k=3;
if (k>N)
{
	
}
else {

You've just initialized N=13 and you've just defined k = 3. In no way do these values change in any kind of overall loop structure so you know it's going to be false, so why have the if statement at all?

EDIT: I tried fine tuning the parameters to ax = 8, bx = 9, x3 = 11.5 and got output.

Function minimum is 0.000000

for X= 9.381965

I think with xmax = 1000 it was just too far out, though I'm not sure why it conked out at x = 150ish.

I am trying to go through line by line and my math is correct it may be an error with my pointers or one of my loops.

the xmax should not affect the final number it should be a check.

thank you so much for all the help so far.

I have attached a pdf of what the calculation should look like by hand and the flow charts at the end.

I have not added in the quadratic approximation yet but I want to get this part of the program working first.

thanks

I got it to run correctly I had to add the goto statement back in my code so it kept iterating until k=13.

now i just have to add then quadratic approximation.

thanks again for all the help

I got it to run correctly

Cool

I had to add the goto statement back in my code

As an "extra credit" just for yourself try to see if you can write it without using one :)

Anyway, you worked hard on it so I hope it goes well. Post back if you have any problems.

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.