Sam Virgo - 1 month ago 7

Dart Question

I am writing a program that needs to find the maximum value of X that satisfies the 2 following equations:

9x < 10

7.5x < 8

Can anyone advise on the best way to go about this? I am writing the program in Dart but would appreciate examples/advice in any language.

This is what I have done currently but I am not 100% sure this is correct:

`double Xval = 10/9;`

if(7.5 * Xval > 8)

Xval = 8 / 7.5;

Please note the program would have to work if we changed any or all of the numbers (e.g. the 9, 10, 7.5 or the 8).

Answer

It needs some mathematical logic.

1)First find value of `x`

by replacing `<`

to `=`

. Eg. Find `x`

in `9x=10`

.

2)Find minimum of both the solution `x`

.

3)This minimum value will satisfy both the equation its obvious but we have replaced `<`

with `=`

so we need to subtract a smallest value by which we can find maximum `x`

which satisfy the original equation.

4)So subtract the value `0.0001`

from minimum if you want 4 decimal point precision. Generally subtract `(10)^(-DecimalPointPrecision)`

value ,so here `DecimalPointPrecision`

is equal to `4`

.

5)This value you get will satisfy both the equation and it will be the maximum value of x.

I have written a code in java implementing this logic.

```
import java.util.Scanner;
class SolveEquation
{
public static void main(String [] args)
{
float ip1_left;
float ip1_right;
float ip2_left;
float ip2_right;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the multiplier of x of 1st equation:");
ip1_left=sc.nextFloat();
System.out.print("Enter the constant of 1st equation:");
ip1_right=sc.nextFloat();
System.out.print("Enter the multiplier of x of 2nd equation:");
ip2_left=sc.nextFloat();
System.out.print("Enter the constant of x of 2nd equation:");
ip2_right=sc.nextFloat();
float ans1=ip1_right/ip1_left;
float ans2=ip2_right/ip2_left;
float min=ans1;
if(ans2<ans1)
min=ans2;
//If you want 4 decimal precision then print 4 digits after point and subtract 0.0001 (where 1 is placed on 4th place after decimal point).
System.out.printf("\nMaximum value of x is %.4f",min-0.0001);
}
}
```

Source (Stackoverflow)

Comments