How to find minimum range to put in a specific function

How to find minimum range to put in a specific function



I pretty new to programming and just got enrolled in a college.
So far I've learned some basic loops, but the question is how do i determine the minimum range of a specific function?



Here's the case:
"Choose the first interval by finding a and b such that 𝑓(𝑎)∙𝑓(𝑏)<0 "



and the function that i have to use is: 8−4.5(𝑥−sin𝑥)=0



Now I was asked to input 2 numbers in that function and multiply it.



Here's example:


import java.util.Scanner;

public class Main {
public static void main(String args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();

System.out.println(function(a)*function(b);

public static double function(double x)
double resultF = 0;
resultF = 8 - (4.5 * (x - Math.sin(x)));
return resultF;



which I already have make a class for it.



Now the problem is how do I find the minimum value if I were to input 2 (function(a)*function(b)) to be as close as zero.



So it begins with making user input 2 random numbers. Then both of them will have to go through 8−4.5(𝑥−sin𝑥)=0 function.
After the numbers have been calculated then you take both of that "calculated numbers" and multiplied it together.
What I am trying to do here is trying to make a loop that would automatically shorter the interval of the "input" and making them as close to zero as possible. (Referring to the 8−4.5(𝑥−sin𝑥)=0 function)



So far what I can do is:



package com.company;


import java.util.Scanner;

public class Main {
public static void main(String args)
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
double resultA = function(a);
double resultB = function(b);
double newA = 0;
double newB = 0;

System.out.println(resultA * resultB);
System.out.println(resultA*resultB);
if (resultA*resultB >=0)
if(a<b)
while(function(a)*function(b)>=0)
newA = a++;
newB = b--;


else if(a > b)
while(function(a)*function(b)>=0)
newA = a--;
newB = b++;






I know that minimum interval is 3 and 2 but I don't know how to get my loop work.



Thank you for your replies !



Every Help is appreciated!
Thank you





which minimium value?
– The Scientific Method
Sep 5 '18 at 17:26





soif i good unserstand you want the shorstest interval were the fontion change of sign?
– Arnault Le Prévost-Corvellec
Sep 5 '18 at 17:29





What I am trying to do is making a loop that would make the interval as close to 0 as possible. The loop refers to this "8−4.5(𝑥−sin𝑥)=0" .
– Thethawat Kongyu
Sep 5 '18 at 17:30





you want to solve aproximately the equation ? get a result that is the closest to the matematical one ?
– Arnault Le Prévost-Corvellec
Sep 5 '18 at 17:31





or get the intervale that match which is the closest from 0?
– Arnault Le Prévost-Corvellec
Sep 5 '18 at 17:33




1 Answer
1



To fully answer completely to your question if you want to get the shortest intervalle the intuitive methods is to reduce it by a step defined like this :


public static void main(String args)
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
double step = sc.nextDouble();
if (a > b)
double tmp = a;
a = b;
b = tmp;

double bResult = function(b);
while (function(a) * bResult < 0)
a += step;

a -= step;
double aResult = function(a);
while (aResult * function(b) < 0)
b -= step;

b += step;
System.out.println(a + " " + b);
System.out.println(function(a) + " " + function(b));
sc.close();



but if you want a precise result then the step is little then the computation can be very long ... A solution consists to use Dichotomy . The concept is simple : reduce the interval by divising le range by 2 until the range is shorter than your wanted interval like this :


public static void main(String args)
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
double step = sc.nextDouble();

if (a > b)
double tmp = a;
a = b;
b = tmp;

while (Math.abs(a - b) > step)
double mid = (a+b) / 2;
if (function(mid) == 0)
System.out.println(mid);
return;

if (function(a) * function(mid) < 0)
b = mid;
else
a = mid;


System.out.println(a + " " + b);
System.out.println(function(a) + " " + function(b));
sc.close();



Hoping that's will help you.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)