473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

finding the max of a function

8
Here is my code. the function is 500x-10x^2. i am trying to find the largest fx, call it max and print it.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(void)
  5.  
  6. {int fx;
  7.  int x=0;
  8.  int max;
  9.  int a=50;
  10.  int fxnext;
  11.  
  12.  
  13.  printf("f(x) x.\n");
  14.  printf("-----\n");
  15.  
  16.  while (x<a)
  17.  {
  18.      (x=x++);
  19.      (fx=(500*x)-(10*x*x));
  20.      (fxnext=(500*(x+1))-(10*(x+1)*(x+1)));
  21.     if (fx>fxnext)     
  22.      printf("%d %d\n",x,fx);}
  23.  }
so this calculates the function and results when x is between 1 and 50. Now I have to find the max value of the function or max of fx. I am trying to compare fx to fxnext and when fxnext is no longer larger than fx print that number. I have been at if for awhile and can't figure this one out. please help.
Feb 3 '08 #1
3 1448
JosAH
11,448 Expert 8TB
The function f(x) = -10x^2+500x definitely has one single maximum: where
f'(x) = -20x+500 == 0. So you have to solve -20x+500 == 0 w.r.t. x.

kind regards,

Jos
Feb 3 '08 #2
jimix
8
yes. the instructor asks us not to use calculus rather compare values for fx and then print the maximum value. that is where I am stuck.
Feb 4 '08 #3
Without using the derivative, you'd need to continue the loop without bounds, and have it end when fxnext is less than fx. I'd suggest initializing both of these so that fxnext is greater than fx, and then use that as your while condition, so that you can repeat it indefinitely (at least in theory). Something like this:

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     int x=0;
  4.     int fx = 0;
  5.     int fxnext = 1;
  6.  
  7.     printf("f(x) x.\n");
  8.     printf("-----\n");
  9.  
  10.     while (fxnext > fx)
  11.     {
  12.         fx = (500*x) - (10*x*x);
  13.         x = x + 1;
  14.         fxnext = (500*x) - (10*x*x);
  15.     }
  16.     x = x - 1;
  17.     printf("%d %d\n",x,fx);
  18. }
That should cause the loop to repeat until fxnext is less than fx. This would make fx the max of the function (after the loop x is the value that gives fxnext, so you need to decrease it afterwards to).

The only problem is that if the function doesn't have a max, or it's very large, you would run into overflow problems with x (although you might be more likely to find overflow problems with fx and fxnext). Also, this assumes that the max of the function occurs at a positive x value. To find the max in either direction you may want to run a loop in both directions (one increasing x the other decreasing it), and then compare the maxes found by each loop. Although you'd still have the potential overflow problems.
Feb 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kevin Thorpe | last post by:
I don't suppose anyone knows of a script/program to try and identify where variables are used assuming register_globals is on? I'm trying to fix an application and would rather not turn it on as...
3
by: KL | last post by:
Well, I am back. This time our assignment has us filling a vector and then timing how long it takes to find a spot in the vector to insert a new item, and the time required to insert the item...
6
by: SSG | last post by:
Hai All! I need the optimized code for finding the string length. i dont want to use strlen function......... can anyone know reply........ By S.S.G
2
by: FR3AK | last post by:
Hi. does anyone have any idea of how to find the mac address of a remote networking device? I've worked with WMI and it works fine as long as the target machine is a Windows computer, however,...
15
by: cwsullivan | last post by:
Hi gang, I have a grid full of particles in my program, and I want to find an angle between two particles. I'm having trouble because it seems like the answer depends on whether or not the target...
2
by: lcaamano | last post by:
We have a tracing decorator that automatically logs enter/exits to/from functions and methods and it also figures out by itself the function call arguments values and the class or module the...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
19
by: Kirk Strauser | last post by:
Given a class: how can I find its name, such as: 'foo' I'm writing a trace() decorator for the sake of practice, and am trying to print the name of the class that a traced method belongs...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
2
choke
by: choke | last post by:
These are just a few snippets of my entire function, but the idea is that user will input an integer n, my function will create an array of n integers. Each element in the array is equal to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.