GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Sep-2005, 22:22
EngineerFORhire EngineerFORhire is offline
New Member
 
Join Date: Sep 2005
Posts: 15
EngineerFORhire is on a distinguished road

Powell Method


Hey all Im trying to write a program using powells method to minimize a polynomial. Im fairly new so any help would be greatly appreciated. Thanks
CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>



double powell(double x1)
{
       double func(double xa);
       double fmin(double x1a, double x2a, double x3a);
       double a1(double x1a, double x2a, double x1, double x2);
       double a2(double x1a, double x2a, double x3a, double x1, double x2, double x3);
       double xopt(double x1, double x2, double a1, double a2);
       double fopt(double xopta);
       double conv(double fopt, double fmina);
       double check(double fopt);


double x2 = x1 + 1;
double x1a = func(x1);
double x2a = func(x2);
if (x1a > x2a);
{
   {
   double x3 = x1 + 2;
   }
else 
     {
           double x3 = x1 - 2;
     }
}
double x3a = func(x3);
double fmina = fmin(double x1a, double x2a, double x3a);
double a1a = a1(x1a, x2a, x1, x2);
double a2a = a2(x1a, x2a, x3a, x1, x2, x3);
double xopta =  xopt(x1, x2, a1, a2);
double conva = conv(fopt, fmina);
double checka = check(fopt);
powell = checka;
}





double func(double xa)
{
double xa;
xa=10*x^4-19*x^2-2*x+5;   
}

double fmin(double x1a, double x2a, double x3a)
{
           
                  if (x1a<x2a)
                  {
                          if (x1a<x3a)
                          {
                             fmin = x1a;
                          }
                          else
                          {
                              fmin = x3a;
                          }
                  }
                  else
                  {
                      if (x2a < x3a;)
                      {
                             fmin = x2a;
                      }
                      else
                      {
                          fmin = x3a
                      }
                  }
                             
}

double a1(double x1a, double 2a, double x1, double x2)
{
       a1 = ((x2a - x1a) / (x2 - x1));
}

double a2(double x1a, double x2a, double x3a, double x1, double x2, double x3)
{
       ((((x3a-x1a)/(x3-x1)) - ((x2a - x1a)/(x2-x1))) / (x3 - x2));
}

double xopt(double x1, double x2, double a1, double a2)
{
      xopt = (((x1 + x2)/2) - (a1/(2*a2)))
}

double fopt(double xopta)
{
       fopt = func(xopta);
}

double conv(double fopt, double fmina)
{
       conv = abs((fopt - fmina)/fmina);
}

double check(double fopt)
{
       if (conv =< .001)
       {
                check = fopt;
       }
       else
       {
           x3 = x2;
           x2 = xopt;
           x3a = func(x3);
           x2a = func(x2);
           x1a = func(x1);
           fmina =fmin(x1a, x2a, x3a);
           a1a = a1(x1a, x2a, x1, x2);
           a2a = a2(x1a, x2a, x3a, x1, x2, x3);
           fopt = xopt(x1, x2, a1, a2);
           conv(fopt, fmina);
           check(fopt, fmina);
       }
}


main()
{
double x1;
cout<<"input x1"<<endl;
cin>>x1;
cout<< powell(x1);
}
end();
Last edited by admin : 30-Sep-2005 at 23:55. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 30-Sep-2005, 22:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Powell Method


Quote:
Originally Posted by EngineerFORhire
Hey all Im trying to write a program

So: what is your question? (Saying, "I'm trying to write a program," isn't really a question is it?) What would you like us to do?

Have you tried to compile your code?

If you are really new, then I would respectfully suggest you learn fundamentals with simple functions before getting into some numerical analysis that might be more difficult to debug. You don't have to waste time writing throw-away stuff (like, for example, writing a function that takes two numbers and returns the sum of the numbers), but for real beginners, maybe that's not a bad way to learn fundamentals of functions and how they are used in C.

You can take your functions (the simple ones), write and test them one at a time. For example, here is a testbed for your func():

CPP / C++ / C Code:
#include <iostream>
using namespace std;
int main()
{
    double func(double xa)
    double x;
    double y;

    x = 1;
    y = func(x);
    cout << "func(" << x << ") = " << y << endl;

    return 0;
}


double func(double xa)
{
    double xa;
    xa=10*x^4-19*x^2-2*x+5;   
}

Now test this.
Does it compile? Do you get any warnings or errors? Do you understand what the compiler is telling you? Can you execute the program?

If you have any problems, then show what you got, and tell us what you expected to get and tell us what you don't understand. (You might also mention what compiler/Operating System you are using; sometimes it makes a difference to people trying to help.)

If you get this function to work OK for x = 1, try other values of x (or use cin>> to get values from the user).

Then on to the next.

Regards,

Dave
  #3  
Old 30-Sep-2005, 23:04
EngineerFORhire EngineerFORhire is offline
New Member
 
Join Date: Sep 2005
Posts: 15
EngineerFORhire is on a distinguished road

Re: Powell Method


Thank you for your reply. I am using Dev-C++ compiler. I need a function where if I press any key it will proceed to the next action. Also, for some reason it wont even let me define x3. The else function looks fine to me.

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>



double powell(double x1)
{
       double func(double xa);
       double fmin(double x1a, double x2a, double x3a);
       double a1(double x1a, double x2a, double x1, double x2);
       double a2(double x1a, double x2a, double x3a, double x1, double x2, double x3);
       double xopt(double x1, double x2, double a1, double a2);
       double fopt(double xopta);
       double conv(double fopt, double fmina);
       double check(double fopt);


double x2 = x1 + 1;
double x1a = func(x1);
double x2a = func(x2);
if (x1a > x2a);
{
   {
   double x3 = x1 + 2;
   }
else 
     {
           double x3 = x1 - 2;
     }
}
double x3a = func(x3);
double fmina = fmin(double x1a, double x2a, double x3a);
double a1a = a1(x1a, x2a, x1, x2);
double a2a = a2(x1a, x2a, x3a, x1, x2, x3);
double xopta =  xopt(x1, x2, a1, a2);
double conva = conv(fopt, fmina);
double checka = check(fopt);
powell = checka;
}





double func(double xa)
{
double xa;
xa=10*x^4-19*x^2-2*x+5;   
}

double fmin(double x1a, double x2a, double x3a)
{
           
                  if (x1a<x2a)
                  {
                          if (x1a<x3a)
                          {
                             fmin = x1a;
                          }
                          else
                          {
                              fmin = x3a;
                          }
                  }
                  else
                  {
                      if (x2a < x3a);
                      {
                             fmin = x2a;
                      }
                      else
                      {
                          fmin = x3a
                      }
                  }
                             
}

double a1(double x1a, double 2a, double x1, double x2)
{
       a1 = ((x2a - x1a) / (x2 - x1));
}

double a2(double x1a, double x2a, double x3a, double x1, double x2, double x3)
{
       ((((x3a-x1a)/(x3-x1)) - ((x2a - x1a)/(x2-x1))) / (x3 - x2));
}

double xopt(double x1, double x2, double a1, double a2)
{
      xopt = (((x1 + x2)/2) - (a1/(2*a2)))
}

double fopt(double xopta)
{
       fopt = func(xopta);
}

double conv(double fopt, double fmina)
{
       conv = abs((fopt - fmina)/fmina);
}

double check(double fopt)
{
       if (conv =< .001)
       {
                check = fopt;
       }
       else
       {
           x3 = x2;
           x2 = xopt;
           x3a = func(x3);
           x2a = func(x2);
           x1a = func(x1);
           fmina =fmin(x1a, x2a, x3a);
           a1a = a1(x1a, x2a, x1, x2);
           a2a = a2(x1a, x2a, x3a, x1, x2, x3);
           fopt = xopt(x1, x2, a1, a2);
           conv(fopt, fmina);
           check(fopt, fmina);
       }
}


main()
{
double x1;
double y;
cout<<"input x1"<<endl;
cin>>x1;
y= powell(x1)
cout<< "Answer is " << y;

return 0;
}
  #4  
Old 30-Sep-2005, 23:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Powell Method


Quote:
Originally Posted by EngineerFORhire
Thank you for your reply. I am using Dev-C++ compiler. I need a function where if I press any key it will proceed to the next action. Also, for some reason it wont even let me define x3. The else function looks fine to me.

I hate to repeat myself but:

Quote:
Originally Posted by davekw7x
f you have any problems, then show what you got, and tell us what you expected to get and tell us what you don't understand.

When you say, "It won't even let me define x3," I assume that you got an error message somewhere. Where? You have lots of lines with x3. Which one gave the error message?

On the other hand, I don't intend to go through 139 lines of code and probably 50 or so error/warning messages for you. Try to understand that I'm not just trying to give you a hard time, I am trying to make the point that You (yes, You) are the best resource for debugging. People on this Forum are here trying to help. Most of us feel that the best way to help is to get you to ask the right question(s) and then we will try to help you understand the answer(s).

The way I tried to help is this:

Quote:
Originally Posted by davekw7x
You can take your functions (the simple ones), write and test them one at a time. For example, here is a testbed for your func():

If you don't want to start with the particular function of yours that I gave as an example, then pick another. If you want to go on floundering with many many lines of code that you don't understand, well I can't think of anything else to say.

So, here's one more attempt at showing an approach that is more likely to get some progress for you:

Write a short main() program that tests one function. If you get any errors or warnings that you don't understand then:

1. Post the exact complete code that you tested.

2. Post the exact error/warning messages that you don't understand (copy/paste them into your post; don't paraphrase).

Regards,

Dave
  #5  
Old 01-Oct-2005, 00:07
EngineerFORhire EngineerFORhire is offline
New Member
 
Join Date: Sep 2005
Posts: 15
EngineerFORhire is on a distinguished road

Re: Powell Method


Hey David, thanks for the help. I cleaned up the code a bit and reduced my errors to roughly 15 compared to about 50 to begin with. I attached the error log. The errors that I cant seem to understand are as follows.

Initially, I cant get my F(x3) to compute,

CPP / C++ / C Code:
if (x1a > x2a){
   
           double x3 = (x1 + 2);
   }
else{
   
           double x3 = (x1 - 2);
   }

double x3a = func(x3);

(the value "2" stands for deltax*1 but since I wont be using any thing other than 1 for delta x im keeping it simple)

Secondly, My fmin function will not work:

CPP / C++ / C Code:
double fmin(double x1a, double x2a, double x3a)
}
           
                  if (x1a<x2a){
                  
                          if (x1a<x3a){
                          
                             fmin = x1a;
                          }
                          else{
                          
                              fmin = x3a;
                          }
                  
                  else{
                  
                      if (x2a < x3a){
                  
                      
                             fmin = x2a;
                      }
                      else{
                      
                          fmin = x3a
                      }
                  }

This seems right to me and causing me much frustration. Any ideas?

Thanks,

David
Attached Images
File Type: jpg error.JPG (79.8 KB, 11 views)
Last edited by LuciWiz : 01-Oct-2005 at 07:49. Reason: Please insert your C code between [c] & [/c] tags
  #6  
Old 01-Oct-2005, 00:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Powell Method


Quote:
Originally Posted by EngineerFORhire
Hey David, thanks for the help. I cleaned up the code a bit and reduced my errors to roughly 15 compared to about 50 to begin with. I attached the error log. The errors that I cant seem to understand are as follows.

Initially, I cant get my F(x3) to compute,
What does the program tell you? What do you mean "I can't get my F(x3) to compute"? I don't see any function F() in this post or any of your other posts.

Quote:
Originally Posted by EngineerFORhire
Secondly, My fmin function will not work:

This seems right to me and causing me much frustration. Any ideas?

What do you mean "will not work"? What values did you give it? What values did it return? I don't see how I can help you understand if I don't know what you don't understand.

Your first error message is about line 31, and is in function powell(). The code snippets that you post don't show me whether they are part of function powell().

Other error messages are talking about xopt() I don't see any xopt() in the code of this post. I seem to remember a function called xopt() in a previous post, but I have no idea where it is in the code that you are asking about now. There is no point in my trying to guess what you are currently working with. If you want to compile xopt() by itself along with a test version of a main() program that calls the function, and you want to understand what's wrong, then post that code (and only that code) along with its error messages or other unexpected behavior.

If you insist on trying to compile a whole bunch of incorrect code without understanding the very first thing, then I can't help you. I have suggested twice how I would approach the problem. If you don't want to do it that way, that's OK, but I simply am out of ideas how to help. Maybe someone else on this forum can jump in where I have failed.

In order to help the next helper, I respectfully suggest that you do the following:

If you want to test fmin(), then
1. Write a short main() that calls fmin() with various values of arguments and prints out the value returned from fmin() each time.
2. Make a file that has only fmin() and main(). Compile and execute.

If the compiler gives you error messages, post the exact error messages along with the complete code. Maybe someone can help you understand.

If your test program compiles OK, but doesn't give the results that you expect:

1. Post the exact code that you are testing --- everything, not just part.
2. Post the exact input that you gave and the output that you get.
3. Tell the person trying to help you what you expected to get and what you don't understand about what you got.

I am really sorry I couldn't help. Sometimes I nail it, and sometimes --- not so much.

Regards,

Dave

(Over and Out)
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-member method needs info from class... how? Elsydeon C++ Forum 3 19-Sep-2005 15:13
TextBox->RefreshText () method??? richiemac .NET Forum 3 23-May-2005 02:29
Simulation - Composition method wu_weidong C Programming Language 7 13-Mar-2005 09:05
calling abstract base class method calls draw instead achoo FLTK Forum 1 19-Dec-2004 09:38
regarding main method jerry C++ Forum 18 09-Mar-2004 18:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:06.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.