Quadratic equation calculator not working - c#

I would like some help with my code.
This is what I've done so far:
using System;
namespace quadratic_equcation
{
class Program
{
static void Main(string[] args)
{
float a = float.Parse(Console.ReadLine());
float b = float.Parse(Console.ReadLine());
float c = float.Parse(Console.ReadLine());
float D = b * b - 4 * (a * c);
double dRoot = Math.Sqrt(D);
double x1 = (-b + dRoot) / 2 * a;
double x2 = (-b + dRoot) / 2 * a;
Console.WriteLine("x1 = {0) x2 = {1}", x1, x2);
Console.ReadLine();
}
}
}
Everything looks good. I got no errors but when I run it I get this error:
How can I fix it ?
And one more question. I couldn't use float with 'dRoot', 'x1' and 'x2' because I can't convert it from double to float.. How can i do this

Console.WriteLine("x1 = {0) x2 = {1}", x1, x2);
you have a typo there... {0)
change it for
Console.WriteLine("x1 = {0} x2 = {1}", x1, x2);
PS: the roots signs are faulty too (one positive one negative)
double x1 = (-b + dRoot) / 2 * a;
double x2 = (-b - dRoot) / 2 * a;

You have typo: {0) in format string.
Console.WriteLine("x1 = {0} x2 = {1}", x1, x2);
Moreover, you have error in algorithm: x1 is same as x2.
double x1 = (-b + dRoot) / 2 * a;
double x2 = (-b - dRoot) / 2 * a;

Related

Geographic coordinate distance calculation with haversine formula gives the wrong output

I'm creating a distance calculator in c# using the haversine equation to calculate the distance between longitudes and latitudes but it is giving the wrong output can anyone see why?
the first long and lat values are for a place in Wales (Bangor) and the other is for a place in England (Manchester)
Here is the code:
using System;
public static class Program
{
static double toRadians(double angle)
{
return (angle * Math.PI) / 180;
}
static double CalcDistance(double lon1, double lon2, double lat1, double lat2)
{
lon1 = toRadians(lon1);
lon2 = toRadians(lon2);
lat1 = toRadians(lat1);
lat2 = toRadians(lat2);
//haversine formula
double dlat, dlon;
dlat = lat2 - lat1;
dlon = lon2 - lon1;
double a = Math.Pow(Math.Sin(dlat / 2), 2) *
Math.Cos(lat1) * Math.Cos(lat2) *
Math.Pow(Math.Sin(dlon / 2), 2);
double c = 2 * Math.Asin(Math.Sqrt(a));
// earths radius is KM, use 3956 for miles
double earthRadius = 6371;
return (c * earthRadius);
}
static void Main(String[] args)
{
double lat1, lat2, lon1, lon2;
lon1= 53.222469;
lat1 = -4.129424;
lon2 = 53.244697;
lat2 = -2.13195;
Console.WriteLine(CalcDistance(lon1, lon2, lat1, lat2) + " KM");
}
}
The output given is 0.04301075336978381 KM when the output should be roughly 130KM
The error is a * vs + (the first one in CalcDistance), but here's a direct conversion from https://www.movable-type.co.uk/scripts/latlong.html, for reference (also adding this to the static double toRadians(this double angle) so it works as an extension method):
static double CalcDistance(double lon1, double lon2, double lat1, double lat2)
{
const double R = 6371;
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2 - lat1).toRadians();
var Δλ = (lon2 - lon1).toRadians();
var a = Math.Sin(Δφ / 2) * Math.Sin(Δφ / 2) +
Math.Cos(φ1) * Math.Cos(φ2) *
Math.Sin(Δλ / 2) * Math.Sin(Δλ / 2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var d = R * c;
return d;
}

Lambert72 to LatLong C#

I'm searching for an algorithm to convert Lambet72 coordinates to lat/long.
So for example Lambert72 coordinates 148990,169450 must be converted into 50.83546802746704, 4.354415218851164 but most of the algorithm have a little offset (see attachments).
Image 1
Image 2
This is one of the algorithms I found which is close, but still have an error.
Someone has got a better algorithm?
static void Lambert72ToLatLong(double x, double y, ref double longitude, ref double latitude)
{
const double n = 0.77164219;
const double F = 1.81329763;
const double thetaFudge = 0.00014204;
const double e = 0.08199189;
const double a = 6378388;
const double xDiff = 150000;
const double yDiff = 5400088.44;
const double theta0 = 0.07604294;
double xReal = xDiff-x;
double yReal = yDiff-y;
double rho = Math.Sqrt(xReal*xReal + yReal * yReal);
double theta = Math.Atan(xReal/-yReal);
longitude = (theta0 + (theta + thetaFudge) / n) * 180 / Math.PI;
latitude = 0;
for(int i=0; i<10; ++i)
{
latitude = (2 * Math.Atan(Math.Pow(F * a / rho, 1 / n) * Math.Pow((1 + e * Math.Sin(latitude)) / (1 - e * Math.Sin(latitude)), e / 2))) - Math.PI / 2;
}
latitude *= 180 / Math.PI;
}
It seems you are trying to convert Lambert72 (Belgium Dattum 72 LCC 3p) to
WGS84 GPS coordinates. Thus, after converting to spherical latitude and
longitude coordinates, an additional step has to be performed to end up
with the WGS84 coordinates. Here is an alternative code in C# with the full conversion
of Lambert72 to WGS74 which complies with the required decimal accurary, as
you easily can verify for the coordinates presented in your quoted images.
static void Lambert72toWGS84latlong(double X, double Y)
{
double LongRef = 0.076042943; //
double nLamb = 0.7716421928;
double aCarre = Math.Pow(6378388.0,2.0);
double bLamb = 6378388.0 * (1.0 - (1.0 / 297.0));
double eCarre = (aCarre - Math.Pow(bLamb, 2.0)) / aCarre;
double KLamb = 11565915.812935;
double eLamb = Math.Sqrt(eCarre);
double eSur2 = eLamb / 2.0;
double Tan1 = (X - 150000.012) / (5400088.437 - Y);
double Lambda = LongRef + (1.0 / nLamb) * (0.000142043 + Math.Atan(Tan1));
double RLamb = Math.Sqrt(Math.Pow((X - 150000.012) , 2.0) + Math.Pow ((5400088.437 - Y) ,2.0));
double TanZDemi = Math.Pow((RLamb / KLamb),(1.0 / nLamb));
double Lati1 = 2.0 * Math.Atan(TanZDemi);
double eSin;
double Mult1, Mult2, Mult;
double LatiN, Diff;
double lat, lng ;
int i=0;
do
{
eSin = eLamb * Math.Sin(Lati1);
Mult1 = 1.0 - eSin;
Mult2 = 1.0 + eSin;
Mult = Math.Pow((Mult1 / Mult2) , (eLamb / 2.0));
LatiN = (Math.PI / 2.0) - (2.0 * (Math.Atan(TanZDemi * Mult)));
Diff = LatiN - Lati1;
Lati1 = LatiN;
i++;
} while (Math.Abs(Diff)> 0.0000000277777);
lat=LatiN;
lng=Lambda;
double SinLat = Math.Sin(lat);
double SinLng = Math.Sin(lng);
double CoSinLat = Math.Cos(lat);
double CoSinLng = Math.Cos(lng);
double dx = -125.8;
double dy = 79.9;
double dz = -100.5;
double da = -251.0;
double df = -0.000014192702;
double LWf = 1.0 / 297.0;
double LWa = 6378388.0;
double LWb = (1 - LWf) * LWa;
double LWe2 = (2.0 * LWf) - (LWf * LWf);
double Adb = 1.0 / (1.0 - LWf);
double Rn = LWa / Math.Sqrt(1.0 - LWe2 * SinLat * SinLat);
double Rm = LWa * (1 - LWe2) /Math.Pow((1.0 - LWe2 * lat * lat) ,1.5);
double DLat = -dx * SinLat * CoSinLng - dy * SinLat * SinLng + dz * CoSinLat;
DLat = DLat + da * (Rn * LWe2 * SinLat * CoSinLat) / LWa;
DLat = DLat + df * (Rm * Adb + Rn / Adb) * SinLat * CoSinLat;
DLat = DLat / (Rm + 0.0);
double DLng = (-dx * SinLng + dy * CoSinLng) / ((Rn + 0.0) * CoSinLat);
double Dh = dx * CoSinLat * CoSinLng + dy * CoSinLat * SinLng + dz * SinLat;
Dh = Dh - da * LWa / Rn + df * Rn * lat * lat / Adb;
double LatWGS84 = ((lat + DLat) * 180.0) / Math.PI;
double LngWGS84 = ((lng + DLng) * 180.0) / Math.PI;
MessageBox.Show("WGS84-Latitude=" + LatWGS84.ToString("###.######") +
"--WGS84 Longitude=" + LngWGS84.ToString("###.######"));
}
Hope these are helpful.
You have to correct the values for
xDiff
and
yDiff
The correct values should be
xDiff=149910;
yDiff=5400150;
You have also to correct the
i<10
to become
i<5
inside the for-loop for the calculation of the latitude. Then you will get the desired results.
Hope these help!

Iteration of an Unknown Number in C#

Let's say I have a formula which contains a variable that user has to guess. But at the end of formula, that variable is calculated again and if the first one and second one doesn't match, formulation has to be solved again with a new value. Shortly assume that I have a formula like this (mine is much more complex and longer than this);
double y1 = Convert.ToDouble(txtboxPredefinedY.Text);
double x, z, Ort;
double y2 = 0;
while (y1 != y2)
{
x = (Math.Pow(y1, 2)) + 10;
z = (y1 - 2) / 3;
y2 = (x / z);
Ort = (y2 + y1)/2;
y1 = Ort;
if (y1 == y2)
break;
}
txtboxResult.Text = r.ToString();
So the y1 variable I defined first has to match the last variable y2. To achieve this I calculate the whole formula and find a new y1, re-calculate formula.
I want to define a y1 value and let the application correct me. For example this code should return me a value of 3.3158. If first input is 3.3158 than y1 becomes equal to y2.
I couldn't use while iteration correctly. How can I fix this? Or maybe, how should I build my while block to give me exact equation?
When working with Double you should compare with tolerance:
double y1 = Convert.ToDouble(txtboxPredefinedY.Text);
double x, z, Ort;
double y2 = 0;
double tolerance = 0.001;
while (Math.Abs(y1 - y2) >= tolerance) {
x = (Math.Pow(y1, 2)) + 10;
z = (y1 - 2) / 3;
y2 = (x / z);
Ort = (y2 + y1)/2;
y1 = Ort;
}
Comparisons like y1 != y2 as well as y1 == y2 may fail because of round-up errors.

Rotating 3D scatter plot data in WPF not working as expected

I have followed the model presented in this article for displaying scatter plot data in WPF. I can easily rotate the image in the view matrix but I need to rotate the raw data points and do a least squares fit of the z-axis values to the z=0 plane. I only need to rotate the data 5-20 degrees so I don't think I need quaternions to avoid gimble lock. I have tried the following methods and have also tried translating the data to the origin before rotating but it has not worked as expected. The RotateX method seems to work but the other 2 methods seem to squish all the data together either in the y axis or the z axis. I've checked the formulas with about 10 different sites and can't find any errors but the results still don't make sense.
public static Point3D RotateAboutX(Point3D pt1, double aX)
{
double angleX = 3.1415926 * aX / 180;
double x2 = pt1.X;
double y2 = (pt1.Y * Math.Cos(angleX)) - (pt1.Z * Math.Sin(angleX));
double z2 = (pt1.Y * Math.Sin(angleX)) + (pt1.Z * Math.Cos(angleX));
return new Point3D(x2, y2, z2);
}
public static Point3D RotateAboutY(Point3D pt1, double aY)
{
double angleY = 3.1415926 * aY / 180;
double x2 = (pt1.X * Math.Cos(angleY)) - (pt1.Z * Math.Sin(angleY));
double y2 = pt1.Y;
double z2 = (pt1.X * Math.Sin(angleY)) + (pt1.Z * Math.Cos(angleY));
return new Point3D(x2, y2, z2);
}
public static Point3D RotateAboutZ(Point3D pt1, double aZ)
{
double angleZ = 3.1415926 * aZ / 180;
double x2 = (pt1.X * Math.Cos(angleZ)) - (pt1.Y * Math.Sin(angleZ));
double y2 = (pt1.X * Math.Sin(angleZ)) + (pt1.Y * Math.Cos(angleZ));
double z2 = pt1.Z;
return new Point3D(x2, y2, z2);
}
I found my own error. The mistake is in the RotateAboutY() method above. The correct method is like this...
public static Point3D RotateAboutY(Point3D pt1, double aY)
{
double angleY = 3.1415926 * aY / 180;
double x2 = (pt1.Z * Math.Sin(angleY)) + (pt1.X * Math.Cos(angleY));
double y2 = pt1.Y;
double z2 = (pt1.Z * Math.Cos(angleY)) - (pt1.X * Math.Sin(angleY));
return new Point3D(x2, y2, z2);
}
The short treatment of this topic can be found here with the correct formulas. There is a more detailed explanation of this topic on Kirupa.com where the correct formulas are also included.

Run-Time Calculation

i am working on Windows Forms Application. i have three buttons. i have written a method that calculates a new location for each button. but i had some errors (explained after the code). the method is:
Random random = new Random();
public int SetPointLocation()
{
int x1 = x2 - 20;
int x2;
int x3 = x2 + 20;
int y1 = y2 - 1;
int y2 = random.Next(0, 2);
int y3 = y2 + 1;
return x2 = (((x3 - x1) * (y2 - y1)) / y3 - y1) + x1;
}
the errors i get :
Cannot use local variable 'x2' before it is declared.
Cannot use local variable 'y2' before it is declared.
so i rearranged the method's block:
Random random = new Random();
public int SetPointLocation()
{
int x2;
int x1 = x2 - 20;
int x3 = x2 + 20;
int y2 = Convert.ToInt32((picBox.Name).Remove(0, 10));
int y1 = y2 - 1;
int y3 = y2 + 1;
return x2 = (((x3 - x1) * (y2 - y1)) / y3 - y1) + x1;
}
now the errors i get:
"Use of unassigned local variable 'x2'".
The formula i've used is the way of finding the median from a Frequency tables "Statistics".
but 'x2' is unknown and i want to calculate it at run-time, but because 'x2' has no value, i can't set 'x1', and 'x3'. What is the solution for this problem?!
Simply use
int x2 = 0;
Everything needs to be initialized before it can be used. This is a requirement of the language.
Not too close related, but hits it anyways: SO.
It sounds like you really just want to pass x2 in as a parameter. You can then call the function when you do know what x2 is supose to be.
Random random = new Random();
public int SetPointLocation(int x2)
{
int x1 = x2 - 20;
int x3 = x2 + 20;
int y2 = Convert.ToInt32((picBox.Name).Remove(0, 10));
int y1 = y2 - 1;
int y3 = y2 + 1;
// Just return what x2 needs to be
return (((x3 - x1) * (y2 - y1)) / y3 - y1) + x1;
}
x2 is not set before using it.
Random random = new Random();
public int SetPointLocation()
{
int x2; // <- here' the problem
int x1 = x2 - 20;
...
give a value to x2:
x2 = 123;
using a uninitialized variable is not allowed in C#.
the compiler should tell you the place where the error is.

Categories