Derivative of a function in FORTRAN

Click For Summary

Discussion Overview

The discussion revolves around the challenges of implementing a program in Fortran to find the minimum and maximum of a polynomial function of two variables. Participants explore various approaches to parsing polynomials, handling coefficients, and optimizing the function.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant outlines a step-by-step approach to finding the extrema of a polynomial, emphasizing the need for symbolic differentiation and the construction of a Hessian matrix.
  • Another participant suggests storing the polynomial in a CHARACTER variable and discusses the complexities of parsing the polynomial, including recognizing characters and handling multi-digit coefficients.
  • A different viewpoint proposes that if only polynomials are to be used, it may be sufficient to store just the coefficients in a two-dimensional array, thus simplifying the problem.
  • One participant questions the necessity of writing a parser and suggests focusing on using a subroutine for the function to find extrema, highlighting the trade-off between accuracy and computational cost.
  • The original poster expresses uncertainty about proceeding with the project after consulting with their professor, indicating a need for further learning before tackling such a complex task.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to the problem, with no consensus reached on whether to focus on parsing or on simplifying the polynomial representation. The discussion remains unresolved regarding the optimal strategy for implementing the program.

Contextual Notes

Limitations include the complexity of parsing polynomials in Fortran, the potential difficulty in implementing symbolic differentiation, and the varying levels of experience among participants, which may affect their proposed solutions.

Who May Find This Useful

This discussion may be useful for individuals interested in programming in Fortran, particularly those working on optimization problems involving polynomials or those seeking to understand the challenges of parsing mathematical expressions in code.

Antonija
Messages
18
Reaction score
0
[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...
Thank you in advance!
 
Last edited by a moderator:
Physics news on Phys.org
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! :smile:
 
  • Like
Likes   Reactions: Mark44
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.
 
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
Antonija said:
make program that finds minimum/maximum of a function of 2 variables in Fortran
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 :smile:
 
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!
 

Similar threads

Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K