471,334 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

How to solve a function?

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?

Sep 5 '08 #1
10 1603
On Fri, 5 Sep 2008 03:47:26 -0700 (PDT), Bernard.Mangay wrote:
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

--
. ''`. Debian maintainer | http://wiki.debian.org/DavidPaleino
: :' : Linuxer #334216 --|-- http://www.hanskalabs.net/
`. `'` GPG: 1392B174 ----|---- http://snipr.com/qa_page
`- 2BAB C625 4E66 E7B8 450A C3E1 E6AA 9017 1392 B174

Sep 5 '08 #2
On Sep 5, 11:47*am, "Bernard.Mangay" <fcm...@googlemail.comwrote:
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
Sep 5 '08 #3
On Sep 5, 11:49*am, David Paleino <dDOTpale...@gmail.c-o-mwrote:
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
Sep 5 '08 #4
"David Paleino" <dD*********@gmail.c-o-mwrote in message
news:20*********************@gmail.c-o-m...
>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?
As I understand, he wants to find an extremum of a mathematical function
represented as a C# method.
Sep 5 '08 #5
"Bernard.Mangay" <fc****@googlemail.comwrote in message
news:d7**********************************@e39g2000 hsf.googlegroups.com...
>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.
Sep 5 '08 #6
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
Sep 5 '08 #7
JS: http://code.msdn.microsoft.com/solverfoundationfs1

A new one to me, cheers ;-p

Marc
Sep 5 '08 #8
Bernard.Mangay wrote:
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.

--
--
--John
to email, dial "usenet" and validate
(was jclarke at eye bee em dot net)
Sep 5 '08 #9
Is this a homework assignment?
Sep 5 '08 #10
Bernard.Mangay wrote:
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();
}
}
}
Sep 6 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by MAgnus | last post: by
7 posts views Thread by Shapper | last post: by
reply views Thread by Mamatha | last post: by
7 posts views Thread by Nobody | last post: by
9 posts views Thread by fcvcnet | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.