Hello,

I am trying to develop a program and I am stuck on a few portions.

1. I need the program to calculate the gradient of a user entered function.

2. I need to multiply matricies which I think I know how to do but the matricies need to have variable dimensions depending on the number of variables in the user entered function.

3. I need to make the program transpose the matrix.

I have written one program ever and this seems somewhat complex to me, not the math the programming. any starting point for me would be really helpful.

thanks in advance.

Here is the first portion of the program that I am having trouble with

double F(double x) {
return (x-10)*(y-10);

double x0, theta, gamma, ea, er, q, qmax, nstar;

x0=0;
theta=0;
gamma=1;
ea=.0001;
er=.0001;
q=1;
qmax=100;
nstar=2;

double gradf;
int xt;

void gradF(int);

xt=x0;
gradf= gradF(xt);   //Need to figure out how to calc gradient

int height, width, H, I;

height=2;
width=2;
H= [height][width];
I= [height][width];

H=I;  

double s;

s=-H*gradf;

Your code is kind of all over the place. The first thing you should be aware of is the syntax for declaring and defining a function (here is a rough layout):

#include<iostream>
using std::cout; //can also put "using namespace std;" (no quotes)
                           //but that can clutter up your namespace

int myfunc(int a);  //prototype or declaration of a function taking 1 
//integer parameter and returning an int
//note it's outside of main()
int main()
{
  int holder;
  int parameter = 10;
  holder = myfunc(parameter);
  return 0;
}
//following is the definition of the function which is placed outside of 
//main() -- it could be placed above main and then you wouldn't need 
//the prototype up there -- but this is more the convention.
int myfunc(int input)
{
     return (input+1);
}

You seem to have some of it down I just wanted to make sure you had a clearer road map.

As far as the gradient goes, it's simply the partial derivative of the function in each of the variables (all "d"s are partials):

e.g., grad F(x) = dF/dx
        grad F(x,y) = (dF/dx, dF/dy)
        grad F(x,y,z) = (dF/dx,dF/dy,dF/dz)

So for d/dx assume y and z are constants and treat them as such. I'm presuming since it's your course you recall exactly how to do that much better than me but it should be the usual (delta y)/(delta x) = f(x) type business.

Have a go with getting your functions and main correct and then post back and I can give you some links where you can read about dynamic arrays.

Ok I had some of that about the function definition in my program that I didn't copy. I made the changes and added the gradient portion. I copied my entire program this time which has a lot in it and a lot of mistakes I am trying to tackle.

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
#define a 1.61803

//The flow charts kinda explain what I am tring to do I'm sure this is ugly and not the most efficient way to write.
//I have an example in the book of what I am trying to solve


//Function to be analyzed

int F(int x) {
return (x-10)*(y-10);

double x0, theta, gamma, ea, er, q, qmax, nstar;

x0=0;
theta=0;
gamma=1;
ea=.0001;
er=.0001;
q=1;
qmax=100;
nstar=2;

double gradf;
int xt;

void gradF(int);

xt=x0;

grad F(x,y) = (dF/dx, dF/dy);

int height, width, H, I;

height=3;
width=3;
H= [height][width];
I= [height][width];

//figure out how to define I matrix

H=I;  //Matrix multiply

double s;

s=-H*gradf;

//find alphastar to min F(x+alphastar*s)-PROJECT1-this works but I think needs to be modified not sure 


}

void Golden(double xi, double xu, double xmax, double fu, 
double fi, double x1, double f1, double f2, double x2) {



//Bounding Routine 
xmax=50;
fu=F(xu); 
fi=F(xi);
while (fu>fi) {
	x1=xu;
	f1=fu;
	xu=x1*(1+a)-xi*a;
		if (xu>xmax)
		{
		cout << "Unbounded";
		}

		else {

			fu=F(xu);
			if (fu>f1){
			}
			else {
			xi=x1;
			fi=f1;
			}
		} 
}
	

//Golden Section Routine

double N=15;
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:
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);
	goto E;
	}

	else {
	k=k+1;
	xu=x2;
	fu=f2;
	x2=x1;
	f2=f1;
	x1=(1-t)*xi+t*xu;
	f1=F(x1);
	goto E;
	}
}

//Three Point Quadratic Approximation


double a2, a1, a0, b;
a2=((((fu-f1)/(xu-x1))-((f2-f1)/(x2-x1)))/(xu-x2));
a1=((f2-f1)/(x2-x1))-a2*(x1+x2);
a0=f1-a1*(x1)-a2*(x1*x1);
b=a1*a1-(4*a0*a2);

x1=(-a1+sqrt(fabs(b)))/(2*a2);
x2=(-a1-sqrt(fabs(b)))/(2*a2);

f1=F(x1);
f2=F(x2);
double ft1;
double ft2;
double alphastar;

ft1=a0+a1*x1+a2*x1*x1;
ft2=a0+a1*x2+a2*x2*x2;
alphastar=-a1/(2*a2);

printf(" for alphastar= %f\n\n",alphastar);
printf("\n Function minimum is %f\n\n",ft1);
}

int main() {

double x3, f1, f2, x1, fu, fi, x2;
double ax=0; 
double bx=10;
x3=50;
x2=0;
x1=0;
f1=0;
f2=0;
fu=0;
fi=0;
Golden(ax,bx,x3,fu,fi,x1,f1,f2,x2);
}

//convergence portion
double df1, n1, n2, df2;

if alphastar=0;
{
}
else
{
x=x+alphastar*s;
	if q>=qmax;
	{//exit not converged
	}
	else
	{
		df1=fabs(f-f0);
		if df1>ea;
		{
			n1=0;
		}
		else 
		{
			n1=n1+1;
		}
		if n1=nstar
		{//exit converged
		}
		else
		{
			df2=fabs(df1/())//max(fabs(f), 10^10)
			if df2>er;
			{
				n2=0;
			}
			else
			{
				n2=n2+1;
			}
			if n2=nstar;
			{//exit converged
			}
			else
			{//return
			}
		}
	}
}
f0=f;
gradf0=gradf;
gradf=gradF(x);

double y, p;

y=gradf-gradf0;
p=alphastar*s;

double sigma, tau;

sigma=ptranspose*y;//how to do transpose
tau=ytranspose*H*y;

double D;

D=;//eq 3.24 in book

H=H+gamma*D;
q=qmax;

//how to loop back to s=-H*gradf


	
//output

Okaaay. Now I remember :) There are still a lot of the same errors in there as before. You really need to sit down and spend some time with the fundamentals (e.g. array declaration, avoiding goto, if statements). I realize the focus of your course may be the numerical methods themselves but you still have to put the time in on your own to learn the language. Lines 89-126 are still in "no mans land" (global) as far as I can tell. I'm not sure what else to tell you. Write small programs first (i.e., get your routine working in the main() program, test it and then move it out to a function if you have to) and then work your way up.

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.