# Homework Help: Derivative of a function in FORTRAN

1. Feb 24, 2015

### Antonija

[Note from mentor: This thread was originally posted in a non-homework forum, therefore it does not follow the standard homework template.]

------------------------------------------------

Hello.

I have some homework to do. I need to make program that finds minimum/maximum of a function od 2 variables in Fortran. I'm thinking of just do it for a polynom.

For start, I'm researching online, what's the best way.

I imagine it this way:

1. input the polynom and parse it
2. every parsed particle, derivate by the rule
3. partial derivativy by x and y must be ==0
4.solve set of equations
5. get values
6. for every (x,y) which may be minimum or maximum, calculate 2.derivative
7.make Hessian matrix
8. calculate det(H) and then look for first particle in Hessian
9.according to 8), decide if the T(x,y) is minimum, maximum or neither.

The thing that bothers me mostly is 1) and 2). As far as I know, it's very difficult to make symbolic derivative in Fortran, it's much easier to do automatic derivative(in exact point of x and y)
So, at the moment I'm trying to make routine to parse my polynom but I didn't even start yet, because I don't understan exactly what I need to do....
I need to save some polynom into array and then for example with CASE command, look for variables, operators, e.g. in array, but what to do with them?? If I have X^2 +3x+5y, when I find + I need to separate them and save them into the new arrays??

Please, if there is someone who can help me with this... I don't understand the concept...

Last edited by a moderator: Feb 24, 2015
2. Feb 24, 2015

### SteamKing

Staff Emeritus
You can store the input polynomial in a CHARACTER variable.

Parsing is a little tricky. For example, let's say the input polynomial is "x^2 +3*x+5".

The program must examine each character individually and decide what function it performs in the polynomial.

For instance:
Is the current character a letter in the set [a-z] or [A-Z] ?
Is the current character a numeral in the set [0-9] ?
Is the current character a mathematical operator in the set [+ - * / ^] ?
Is the current character a miscellaneous numerical symbol in the set [ . , E] ?

Based on the answers to these initial questions, further sifting of the context might be necessary. For instance, detecting multi-digit coefficients, like '25' or '349'. If you want to express large numbers in scientific notation, then the parser must be able to recognize whatever format you choose for inputting such numbers.

I think you'll find that trying to parse even simple expressions like polynomials will turn your program into more of a parsing program than an optimization program.
While in theory you could write a Fortran program to do this, it's not what the language is best suited for.

You'll spend 90% of your time writing the routines to handle and analyze the input polynomials, and about 10% of your time programming the crunching of the numbers. That's what programming is about sometimes: deciding how many features you can incorporate into a program and how easily (or how difficult) their implementation will be. It's especially challenging if you are trying to finish the program on a deadline.

Good Luck!

3. Feb 25, 2015

### Staff: Mentor

If you are only going to work with polynomials, the function only needs to know the coefficients. To express the polynomial
$$3xy^2 + x^2 + 5xy + 8 y -5 = 0$$
you could set an array c(0:2,0:2) containing
c(0,0) = -5
c(2,0) = 1
c(0,1) = 8
c(1,1) = 5
c(1,2) = 3
with all the other coefficients set to zero. The first index corresponds to the exponent of x, the second that of y.

4. Feb 25, 2015

### BvU

I saw this thread earlier and I wondered why you were turning it into a "writing a parser" exercise, So I second Claudio's suggestion and propose you focus on
Or even stronger: assume f(x,y) is a subroutine you can call (at a cost) and you are to find max and min with a good tradeoff between accuracy and cost. And don't make it too fancy: generations of mathematicians and programmers have worked long and hard on this -- and still are

5. Feb 25, 2015

### Antonija

Thank you all, very very much. Unfortunatley, today I have been talking with my professor and decided not to do it now, because I just passed Fortran basics course, so maybe I should wait a little more, and learn much more before I could even understand, and then write a code for something like this... but I will remember this for some time in soon future! Thanks!