CSharp/C# >> How to solve a function?

by Bernard.Mangay » Fri, 05 Sep 2008 18:47:26 GMT

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?


CSharp/C# >> How to solve a function?

by David Paleino » Fri, 05 Sep 2008 18:49:16 GMT



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

CSharp/C# >> How to solve a function?

by Pavel Minaev » Fri, 05 Sep 2008 18:58:07 GMT


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.

CSharp/C# >> How to solve a function?

by Jon Skeet [C# MVP] » Fri, 05 Sep 2008 18:58:40 GMT


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

CSharp/C# >> How to solve a function?

by Pavel Minaev » Fri, 05 Sep 2008 18:58:49 GMT


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

CSharp/C# >> How to solve a function?

by Jon Skeet [C# MVP] » Fri, 05 Sep 2008 19:01:00 GMT


Possibly not, but I'm pretty sure I understand it.


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

CSharp/C# >> How to solve a function?

by Marc Gravell » Fri, 05 Sep 2008 19:03:09 GMT

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

CSharp/C# >> How to solve a function?

by Marc Gravell » Fri, 05 Sep 2008 19:05:05 GMT

> JS: http://code.msdn.microsoft.com/solverfoundationfs1

A new one to me, cheers ;-p

Marc

CSharp/C# >> How to solve a function?

by J. Clarke » Fri, 05 Sep 2008 21:23:16 GMT


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)

CSharp/C# >> How to solve a function?

by Israel » Sat, 06 Sep 2008 00:37:32 GMT

Is this a homework assignment?

CSharp/C# >> How to solve a function?

by Arne Vajh » Sun, 07 Sep 2008 01:50:56 GMT


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();
}
}
}

Similar Threads

1. help in solving this function object.

I am trying to learn STL but got stuck in for_each().
What I intend to do in the following is to make a list  with each
element as a string.
add the string elements to the list until it has 10 elements. each
element is the same 1234567890. then I want to use for_each to iterate
the list, and for each  to call apply() whihc is create a filen with
name the same as string al;  and within each file, it contains the
content of the list element.
I go stuck in making apply to work. Can anyone help?? the following is
the code, it compiles under vc++ 8.


Thanks


#include <algorithm>
#include <list>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

class makefile{

public:
	void operator()(std::string sth, std::string sth2){

	};
	void apply(std::string s, std::string name){
			std::filebuf buffer;
			std::ostream output(&buffer);
			buffer.open(name.c_str(), std::ios::out);
			output << s << std::endl;
			}
};

int main(){
	std::string s("1234567890");
	std::string al("abc");
	list<std::string> lString;
	for (int i=0; i <= 9; i++) {
		//std::string tmp(itoa(i));
		al = s+ s.at(i);
		lString.push_back(s);
	}
	//for_each();

    for_each(lString.begin(),lString.end(),apply());
	return 0;
}

2. Can you solve this function chaining quiz?

3. Function address vs. pointer to member function (solved)

4. Member function pointer to derived [SOLVED]

5. Using member function pointers as template arguments (solved)

6. Ambiguous template function, best way to solve it...

7. Overloading MFC Functions - solved!

8. Invoking methods on objects returned from functions (solved)