Derivative of a function in FORTRAN

In summary, the student is researching online how to optimize a function of two variables in Fortran. He is struggling with parsing the input polynomial and understanding the concepts behind it. He is also struggling with making a program to find the minimum and maximum of the function.
  • #1
Antonija
18
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
  • #2
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 Mark44
  • #3
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
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:
 
  • #5
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!
 

1. What is a derivative of a function in FORTRAN?

A derivative of a function in FORTRAN is the rate of change of the function with respect to its input variable. It is a mathematical concept used to describe the slope of a curve at a specific point.

2. How is the derivative of a function calculated in FORTRAN?

The derivative of a function in FORTRAN is calculated using the built-in mathematical function DFDX. This function takes in the function name and the input variable as arguments and returns the derivative value.

3. Can FORTRAN handle higher-order derivatives?

Yes, FORTRAN can handle higher-order derivatives by using the D2FDX2 function for the second derivative, D3FDX3 for the third derivative, and so on. These functions can be nested to calculate higher-order derivatives.

4. Is there a specific syntax for writing derivative functions in FORTRAN?

Yes, the syntax for writing a derivative function in FORTRAN is similar to that of a regular function. It starts with the keyword FUNCTION, followed by the function name, input variable, and the keyword DERIVATIVE. The code block within the function should contain the mathematical expression for the derivative.

5. How accurate are FORTRAN's derivative functions?

FORTRAN's derivative functions are highly accurate and reliable. They are based on well-established mathematical algorithms and have been extensively tested and optimized over the years. However, it is always recommended to verify the results and make necessary adjustments for any potential errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
21
Views
308
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
8
Views
478
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
Back
Top