Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Local Minimum and Local Maximum of A Cubic Function
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
randint




PostPosted: Mon Dec 19, 2011 10:26 pm   Post subject: Local Minimum and Local Maximum of A Cubic Function

This program uses the basics of derivatives of cubic functions, which are quadratic functions, to find the local maximum and minimum (if they exist) on any given cubic function in the form f(x)=ax^3+bx^2+cx+d where a, b, c, d are constants and a != 0


localmaxmin.java
 Description:
The program that finds the local max and min of a cubic function

Download
 Filename:  localmaxmin.java
 Filesize:  1.46 KB
 Downloaded:  436 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Tue Dec 20, 2011 9:41 am   Post subject: RE:Local Minimum and Local Maximum of A Cubic Function

I suggest you improve your prompting. "Enter a number" is not very descriptive. Is the first number "a", then "b", then "c"?
I also suggest you comment your code, so I can understand what it's doing without having to actually read the code, and improve your variable names. 2-character variable names might be okay for your teacher, but they are unacceptable in most cases.



Finally, when I run it, the output is NaN for both values, with inputs 1, 2, 3, 4.
randint




PostPosted: Tue Dec 20, 2011 4:04 pm   Post subject: RE:Local Minimum and Local Maximum of A Cubic Function

Well, as to your question of why 1, 2, 3, 4 will return NaN, it is because these inputs represent f(x)=x^3+2x^2+3x+4, the first derivative of this function is a quadratic function that does not have any real root at all. Therefore, it is not possible for this function to have any local maximum or minimum. Graph that function yourself with some technology such as Geometer's Sketchpad and see why the answer is NaN.
Insectoid




PostPosted: Tue Dec 20, 2011 5:37 pm   Post subject: RE:Local Minimum and Local Maximum of A Cubic Function

Oh, rofl. Didn't even consider that. I typed in random numbers and each time I got NaN. I figured at least one must've had a local min/max.

It's nice to see people doing mathy-things instead of just games all the time.
Zren




PostPosted: Tue Dec 20, 2011 8:01 pm   Post subject: Re: Local Minimum and Local Maximum of A Cubic Function

This post is mainly because I've never completely understood this part of polynomials, and me finding out how it works. Figured I just leave it here.

x^0
Ignore this as it's only used for translation.

x^1
x → -∞ then y → -∞
x → +∞ then y → +∞

x^2
x → -∞ then y → +∞
x → +∞ then y → +∞

x^3
The same as x^1. Odds.

x^4
The same as x^2. Evens.


Basically to obtain local min/maxes, we need two Evens or 2 Odds with combating +/- signs.

Say + x^4 - x^2. x^4 added to - x^2.

Now they're both start from zero, however, the rate of increase is different during a specific range for exponents. The ranges are Exponents between 1 and 0 and between 0 and -1 (fractions) are one way different than from 1 → ∞. During that the first range: the larger the exponent, the smaller the value.

Eg:
x (base) = 1/4
x^2 = x * x = 1/4 * 1/4 = 1/16
x^4 = (1/4)^4 = 1/(4^4) = 1/256

As you can see, when x is in this range, the lower exponents are larger, making the equation: f(1/4) = x^4 - x^2 turn out to be a negative as the latter term is bigger. This effect only lasts till near 1/-1 when the larger exponent starts to pick up the pace and kick the little guys butt.

Then you just scale the thing to make it a chaotic mess.

Oh and use WolframAlpha for awesomness.
http://www.wolframalpha.com/input/?i=-x%5E4+%2B+x%5E3+%2B+x%5E2+-+x
randint




PostPosted: Wed Dec 21, 2011 11:11 am   Post subject: RE:Local Minimum and Local Maximum of A Cubic Function

There is this thing, known as the chain rule, you take the exponent of the term, multiplied by the coefficient, then, the exponent on the term of the derivative is 1 less than the one on the original function:
f(x)=1x^3-6x^2+9x+42
f'(x)=(1*3)x^(3-1)+6*2x^(2-1)+9*1x(1-1)
=3x^2-12x+9
=3(x^2-4x+3)
=3(x-1)(x-3)
3^3-6(3^2)+9^3-42=-42
1-6+9-42=-38
(3,-42) and (1,-38) are the points
Take the second derivative of the function you will get f''(x)=6x-12, 6(1)-12 is negative, therefore it is a maximum, 6(3)-12 is positive, so it is a minimum.
Insectoid




PostPosted: Wed Dec 21, 2011 12:48 pm   Post subject: RE:Local Minimum and Local Maximum of A Cubic Function

I don't think you understand the chain rule. It doesn't really apply to x^n where n is constant. It only applies in cases of f(g(x)). If you had something like x^(x+1), you'd use the chain rule.

What you're talking about is just a basic shortcut.
randint




PostPosted: Wed Dec 21, 2011 5:04 pm   Post subject: Local Minimum and Local Maximum of A Cubic Function v. 2

This is an updated version of the program


localmaxmin.java
 Description:
Updated version of local minimum and maximum finder

Download
 Filename:  localmaxmin.java
 Filesize:  2.79 KB
 Downloaded:  311 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
randint




PostPosted: Fri Dec 23, 2011 10:38 am   Post subject: Error in Program

The problem with my program is that, in a way, this program does not calculate derivatives or substitutes values into them correctly, such functions as
f(x)=3x^3+5x^2+2x+6 "has 2 local minima" according to the program, which is bad, anyway, the quadratic formula
randint




PostPosted: Fri Dec 23, 2011 6:31 pm   Post subject: Close Thread

Please do not download any program on this thread, I am the author of the programs, I will be starting a new thread once the problems are fully corrected, it will contain my improved version of this calculator, the program is currently highly defective, DO NOT USE IT! If you have downloaded it, DELETE IT FROM YOUR COMPUTERS! IT IS NOT ACCURATE! IT WILL GIVE YOU A FALSE SENSE THAT CALCULUS IS UNRELIABLE! SORRY FOR THE INCONVINENCE THAT I AM CAUSING YOU.
Aange10




PostPosted: Fri Dec 23, 2011 6:56 pm   Post subject: RE:Local Minimum and Local Maximum of A Cubic Function

o.o
randint




PostPosted: Mon Mar 12, 2012 8:49 pm   Post subject: RE:Local Minimum and Local Maximum of A Cubic Function

Yeah, you are right:

Product rule
if h(x)=f(x)*g(x)
h'(x)=f'(x)*g(x)+f(x)*g'(x)

Quotient rule
if h(x)=f(x)/g(x)
h'(x)=[f'(x)g(x)-f(x)g'(x)]/[g(x)^2]

Chain rule
if h(x)=f(g(x))
h'(x)=f'(g(x))*g'(x)

Power rule
if f(x)=ax^n
f'(x)=anx^(n-1)
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: