Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Help me write a program in C that will fit the values of k into the equation.

formula = 5/1+k + 9/2+k + 13/3+k + 17/4+k -16

The value of k can be negative and an integer; in the case of the above selection method, the result should come out to 0.250000000.

What I have tried:

I tried giving k an initially negative value, and using a loop to add 0.000001 at a time, but for some reason I don’t get the correct value when k is negative or not an integer
Updated 21-Mar-24 20:40pm
k5054 21-Mar-24 16:50pm    
I assume you mean
formula = 5/(1+k) + 9/(2+k) + 13/(3+k) + 17/(4+k) - 16 ?
Rick York 21-Mar-24 17:07pm    
I noted that in my solution also, which I started writing before I saw your comment.
Саша Гайдамак 21-Mar-24 16:50pm    
yes, in my code it is written like this
jeron1 21-Mar-24 17:06pm    
That's a BIG difference, to omit the parenthesis.

I don't think you are writing that correctly. Shouldn't it be 5/(1+k) + 9/(2+k) ... with parenthesis around each denominator term? This detail is very, very important. I would add parenthesis around each term summed so you don't have to rely on interpretation of the precedence list.

I would first try the Newton-Raphson method as described at : Fast and Stable Polynomial Root Finders - Part Two[^] although I am not certain it would work for this equation.

If that doesn't work you could try a binary search approach. First try a succession of values to find two numbers where one answer gives a result above zero and the next one gives a result below zero, or vice versa. Then you try a result in the middle of those two and subsequently keep trying the middle value between two others until you get an answer that is within your tolerance, the epsilon described in my previous reply. In your previous question, you tried successive values between -10000 and 10000, if I recall correctly. You try this method with those as the two initial guesses. If they give results with opposite signs then those are reasonable first guesses. Then keep trying the middle value between guesses.

The key to this approach is picking the right value to check next. What I mean is if -10000 and 10000 are tried first and the results are of opposite signs, then try 0 next. The result of the check with 0 will determine which value you try next. As an example, let's say the result of checking -10000 is negative and the result of 10000 is positive then check 0 next. If the value at zero is negative then try +5000 next. If the value at zero is positive then try -5000 next, and keep splitting the difference like that until you have zeroed in on the result.

ETA: as my comment stated, I decided to try this and I am not sure I understand the formula correctly. The problem is, given the way I wrote it above, there are four asymptotes toward infinity at -1, -2, -3, and -4. I ended up finding the one at -1 first using a binary search.

The question I have is what does it really mean to "fit the values of k into the equation? If it is finding k so that formula is zero then I don't think it is possible because of the asymptotes.
Share this answer
Rick York 21-Mar-24 17:49pm    
Incidentally, this seems like an interesting problem so I decided to give it a try. I found the best initial guesses to start with are -10000 and 0 and let the search begin from there.
CPallini 24-Mar-24 14:24pm    
In fact, a lot can go wrong here at the same time, as Rick has already written. In addition to the problem of choosing the correct search range and step size, the function also has 4 jumps, as the formula already reveals. In addition to the risk of problems when dividing by zero, there are also several possible solutions. If the formula is available as text, these places could be automatically detected, counted and skipped; alternatively, in C++ these places could be intercepted and skipped.
The task of finding suitable k, so that f(k) = 0.25 could be solved like this:
As already discussed in a previous question, if necessary find by brute force, whereby in this case there should be 4 suitable positions. Here, it is advantageous to first get an impression of what is happening: image of the plot[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900