单位向量函数
Unit Vector Function
我正在尝试计算 R² 中的单位向量,使用 JavaScript。
我预计输出为 1,但我得到了 1.949.6。我在这个实现中错过了什么?
function calcHypotenuse(a, b) {
return (Math.sqrt((a * a) + (b * b)));
}
function contructUnitVector(a, b) {
const magitude = calcHypotenuse(a, b);
return (Math.sqrt(a + b / magitude));
}
console.log(contructUnitVector(3, 4)); // 1.949, expected 1
单位向量不是数字,而是...向量。如果给定一个向量在R²中的坐标,那么可以得到对应的单位向量如下:
function vectorSize(x, y) {
return Math.sqrt(x * x + y * y);
}
function unitVector(x, y) {
const magnitude = vectorSize(x, y);
// We need to return a vector here, so we return an array of coordinates:
return [x / magnitude, y / magnitude];
}
let unit = unitVector(3, 4);
console.log("Unit vector has coordinates: ", ...unit);
console.log("It's magnitude is: ", vectorSize(...unit)); // Always 1
我正在尝试计算 R² 中的单位向量,使用 JavaScript。
我预计输出为 1,但我得到了 1.949.6。我在这个实现中错过了什么?
function calcHypotenuse(a, b) {
return (Math.sqrt((a * a) + (b * b)));
}
function contructUnitVector(a, b) {
const magitude = calcHypotenuse(a, b);
return (Math.sqrt(a + b / magitude));
}
console.log(contructUnitVector(3, 4)); // 1.949, expected 1
单位向量不是数字,而是...向量。如果给定一个向量在R²中的坐标,那么可以得到对应的单位向量如下:
function vectorSize(x, y) {
return Math.sqrt(x * x + y * y);
}
function unitVector(x, y) {
const magnitude = vectorSize(x, y);
// We need to return a vector here, so we return an array of coordinates:
return [x / magnitude, y / magnitude];
}
let unit = unitVector(3, 4);
console.log("Unit vector has coordinates: ", ...unit);
console.log("It's magnitude is: ", vectorSize(...unit)); // Always 1