Jonathan Allen
Jonathan Allen

Reputation: 70327

Calculate m 1 ,m 2 for y=m 1 x 1 +m 2 x 2 using Math.NET

Given these values:

x1 = {1, 3, 6, 8} 
x2 = {2 ,8, 5, 10} 
y = {8.6, 30.8, 34.1, 53.8} 

And this formula

y = m1 * x1 + m2 * x2  

Is there a way to determine m1 and m2 using Math.NET or an equivalent C# library?

(The expected result for these values is m1=3.6 and m2=2.5)

Upvotes: 0

Views: 266

Answers (1)

Timothy Shields
Timothy Shields

Reputation: 79551

This is just a system of linear equations: solve Xm = y for m given X and y.

var X = Matrix<double>.Build.DenseOfArray(new double[,] {
    { 1, 2 },
    { 3, 8 },
    { 6, 5 },
    { 8, 10 }
});
var y = Vector<double>.Build.Dense(new double[] {
    8.6,
    30.8,
    34.1,
    53.8
});
var m = X.Solve(y);

Upvotes: 1

Related Questions