Question

I need to compute the first derivative of user-specified function. Program reads function as string from a file and then needs to compute derivative by one variable. Function has more variables, and can be linear or nonlinear. What is the easiest way to do this?

Can calling MATLAB (maybe) be possible or whatever?

Program doesn't have any Internet accesses.

Était-ce utile?

La solution

You can use a symbolic algebra library such as javacalculus.

import java.util.Scanner;
import javacalculus.core.CALC;
import javacalculus.core.CalcParser;
import javacalculus.evaluator.CalcSUB;
import javacalculus.struct.CalcDouble;
import javacalculus.struct.CalcObject;
import javacalculus.struct.CalcSymbol;

public class Demo
{
    public static void main(String[] args) throws Exception
    {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter expression:");
        String expression = in.nextLine();
        // javacalculus uses uppercase function names
        expression = expression.replace("sin", "SIN");
        expression = expression.replace("cos", "COS");

        System.out.println("Differentiate with respect to:");
        String variable = in.nextLine();

        // differentiate
        String command = "DIFF(" + expression + ", " + variable + ")";
        CalcParser parser = new CalcParser();
        CalcObject parsed = parser.parse(command);
        CalcObject result = parsed.evaluate();

        // compute numerical value
        result = subst(result, "a1", 0.0);
        result = subst(result, "a2", 10.0);
        result = CALC.SYM_EVAL(result);

        System.out.println("Result:");
        System.out.println(result);
    }

    static CalcObject subst(CalcObject input, String var, double number)
    {
        CalcSymbol symbol = new CalcSymbol(var);
        CalcDouble value = new CalcDouble(number);
        return CalcSUB.numericSubstitute(input, symbol, value);
    }
}

Sample input and output:

Enter expression:
sin(a1) * 4 * a2 + (a1 + 1)^2
Differentiate with respect to:
a1
Result:
42

Autres conseils

I'm a little bit late, but.

If I'd like to inform you about MathParseKit. https://github.com/B3rn475/MathParseKit

It is a C++ library that can be used to parse and solve functions.

Among all the functionality allow you to derivate a function by a variable.

People, I would be very grateful if you tell me this thing more :) I have function (same as above) and need to calculate it with same those values. But I cann't pare it as CalcObject and then substitute and evaluate it.

    CalcObject result = parsed.evaluate();

    // compute numerical value
    result = subst(result, "a1", 0.0);
    result = subst(result, "a2", 10.0);
    result = CALC.SYM_EVAL(result);

and from subst(..)

    CalcSymbol symbol = new CalcSymbol(var);
    CalcDouble value = new CalcDouble(number);
    return CalcSUB.numericSubstitute(input, symbol, value);

Here I need to pass CalcObeject to subst(...) and then I can evaluate function. But how to do that? From string to CalcObject.

And is there some another solution or function for my problem?

EDIT

I needed to evaluate function from previous posts. My question was, how to do that and can I use Tom's method to do this. I have function, variables, and their values. Dont need to differentiate function, just to evaluate it.

RESOLVED

Maybe not the best way but it works. I made function a (a1+a2+a3)*q from a1+a2+a3 and then I differentiate it by q. Now, my derivative if needed function which I can now evaluate using tom's way. :)

Thank anyway. P.S. If some has better way, I'm here.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top