How to solve a function?

B

Bernard.Mangay

I have a method

double myfunction(double param1){

// code here

}

The Method takes 1 parameter, and returns a double. It is continuous.

Are tehre any functions in C sharp that will help me to find param1
such that myfunction is minimised?
 
D

David Paleino

I have a method

double myfunction(double param1){

// code here

}

The Method takes 1 parameter, and returns a double. It is continuous.

Are tehre any functions in C sharp that will help me to find param1
such that myfunction is minimised?

Am I the only one who didn't understand what he wants? :S

Bernard, would you please explain what do you want to accomplish?

David
 
P

Pavel Minaev

Bernard.Mangay said:
I have a method

double myfunction(double param1){

// code here

}

The Method takes 1 parameter, and returns a double. It is continuous.

Are tehre any functions in C sharp that will help me to find param1
such that myfunction is minimised?

No. C#/VS is not a computer algebra system. If you need to solve such tasks,
you should look at Maple and other similar packages.
 
J

Jon Skeet [C# MVP]

I have a method

double myfunction(double param1){

// code here

}

The Method takes 1 parameter, and returns a double.  It is continuous.

Are tehre any functions in C sharp that will help me to find param1
such that myfunction is minimised?

Certainly not in C# as a language, but there's a new MS library which
might help you:
http://code.msdn.microsoft.com/solverfoundationfs1

I assume you're only looking for a local minimum? It's pretty easy to
come up with a continuous function which would be hard to find the
minimum for programmatically in any sane length of time.

Jon
 
P

Pavel Minaev

David Paleino said:
Am I the only one who didn't understand what he wants? :S

Bernard, would you please explain what do you want to accomplish?

As I understand, he wants to find an extremum of a mathematical function
represented as a C# method.
 
J

Jon Skeet [C# MVP]

Am I the only one who didn't understand what he wants? :S

Possibly not, but I'm pretty sure I understand it.
Bernard, would you please explain what do you want to accomplish?

Consider a function such as x => x*(x-1). That has a minimum value
when x is 1/2. I believe Bernard was trying to get that source value
(1/2) given the method which would compute the target value for any
source.

Jon
 
M

Marc Gravell

No; you would have to do the maths yourself. There are various
frameworks that add code for this.

There used to be a math pack on Lutz Roeder's site, but it seems to
have disappeared now that Reflector has gone to Red Gate... it might
have been a good fit, though!

Marc
 
J

J. Clarke

Bernard.Mangay said:
I have a method

double myfunction(double param1){

// code here

}

The Method takes 1 parameter, and returns a double. It is
continuous.

Are tehre any functions in C sharp that will help me to find param1
such that myfunction is minimised?

There wouldn't be anything in the language itself and I don't think
there's anything in the .NET framework. The solver foundation that
others have mentioned may have something--I haven't had a chance to
look at it.

Beyond that, there are other options.

If you can put up with the terms of license, "Numerical Recipes in
C++", available from Amazon.com, has code samples for this sort of
thing that should run in C# with minimal modification.

Any decent text on numerical analysis should have several algorithms
for this sort of thing--I don't have a recommendation as the ones I
have are out of print.

If you just need to solve the thing and can't do it analyticall or
graphically, Wolfram puts out a product for 165 bucks called
"Mathematical Explorer" that runs on top of an older version of
Mathematica and exposes the engine--while it's not cutting edge it
shouldn't have any problem finding extrema of a continuous function.
 
A

Arne Vajhøj

Bernard.Mangay said:
I have a method

double myfunction(double param1){

// code here

}

The Method takes 1 parameter, and returns a double. It is continuous.

Are tehre any functions in C sharp that will help me to find param1
such that myfunction is minimised?

Not any specific feature in C# or .NET, but the delegate
feature makes it easy to implement Newtons method. See
below for some code to get you started.

Arne

=================================================

using System;

namespace E
{
public class Program
{
public static double f1(double x)
{
return x*x - 2*x + 3;
}
public static double f2(double x)
{
return 1/x + x;
}
private const double START = 10;
private const double SMALL = 0.0000001;
private static double D1(func f, double x)
{
return (f(x+SMALL/2) - f(x-SMALL/2)) / SMALL;
}
public delegate double func(double x);
public static double FindLocalExtrema(func f)
{
double x = START;
double oldx;
do
{
oldx = x;
x = x - D1(f,x) / f(x);
}
while (Math.Abs(x - oldx) > SMALL);
return x;
}
public static void Main(string[] args)
{
Console.WriteLine(FindLocalExtrema(f1));
Console.WriteLine(FindLocalExtrema(f2));
Console.ReadKey();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top