• 11
• 27
• 9
• 20
• 31

# Help with this formula...

This topic is 1904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am not sure how to make this formula work...

Let’s say we had a very weird two dimensional, sinusoidal topography such that z = f(x) = sinx with z the height and x is the distance from some marker. The slope in the x direction

I am trying to convert the scalar field (height versus position) to a vector field (direction and magnitude of greatest slope) mathematically

##### Share on other sites

So you're trying to find the gradient of f (denoted as ?f)?

[attachment=13003:z.png]

Then:

[attachment=13005:solved.png]

Also, did your "The slope in the x direction" sentence get cut off?

I'll be honest, I'm not entirely sure what you're after, and it's possible you're after something different than the ?f, but to be honest your post isn't incredibly clear.

Edited by Cornstalks

##### Share on other sites

Here is what the article says....

We often wish to differentiate a function along three orthogonal axes. For example, imagine we know the topography of a ski area (see Figure A.6). For every location (in say, X and Y coordinates), we know the height above sea level. This is a scalar function. Now imagine we want to build a ski resort, so we need to know the direction of steepest descent and the slope (red arrows in Figure A.6).

To convert the scalar field (height versus position) to a vector field (direction and magnitude of greatest slope) mathematically, we would simply differentiate the topography function. Let’s say we had a very weird two dimensional, sinusoidal topography such that z = f(x) = sinx with z the height and x is the distance from some marker. The slope in the x direction (), then would be d _ dxf(x). If f(x,y,z) were a three dimentional topography then the gradient of the topography function would be:

So from the looks of it yes, a Gradient...

So lets say we have (x,y) as (10, 100)

then

z=sin(x);??

as for that I am not sure what comes after the cosine()....

Thanks!

Edited by MARS_999

##### Share on other sites
The gradient is a vector field. The first coordinate of the vector at (x,y) will be (d/dx)f(x,y) and the second coordinate will be (d/dy)f(x,y). It looks like your source is referring to the vector field that is (1,0) everywhere as x with a hat and (0,1) as y with a hat. It is more standard to refer to them as (d/dx) and (d/dy) with the funny d's that are used for partial differentiation (although that notation can be extremely confusing to non-geometrists).

So if you have (x,y) as (10,100), the gradient of your function f at that point is the vector (cos(10), 0).

I hope that clears things up.

##### Share on other sites

That helps Alvaro, but why is cos(10),0 and not cos(10), 100??

so with a x,y pair of 10,100

the result xyz vector would be??

x,y, z= (cos(10), 100, sin(10)?

##### Share on other sites
Forget z for a moment. You have a function in two dimensions (x and y) and its gradient is a two-dimensional vector field. You can look at the function as being the coordinate z, but then you are talking about the graph of the function, not the function itself.

##### Share on other sites

So is this correct then?

double Gradient(const std::vector<double> &v) {     return sin(v[0]) + 2 * cos(v[1]) - sin(v[2]); }   std::vector<double> v;     v.push_back(1);     v.push_back(1);     v.push_back(1);     std::cout << Gradient(v) << std::endl; //result = 1.0806

but aren't I looking for these values?

     x(0) = 1.570795457
x(1) = 6.423712373e-006
x(2) = 4.712391906

If so how do I get to that  result?

##### Share on other sites
There are several problems with that code. First of all, you probably don't want to use std::vector to represent vectors. Despite the tempting name, std::vector is actually a dynamic array. Vectors are things you can add, subtract and scale. You can't do any of these things with std::vector.

A bigger problem with your code is that you are returning a double from Gradient, but the gradient is a vector, not a real number.

It's also hard to know if your Gradient function does the right thing if I don't know what function it's supposed to be the gradient of.

This is the kind of thing I would write:
#include <iostream>
#include <cmath>

struct Vector3D {
double x, y, z;

Vector3D(double x, double y, double z) : x(x), y(y), z(z) {
}
};

std::ostream &operator<<(std::ostream &os, Vector3D v) {
return os << '(' << v.x << ',' << v.y << ',' << v.z << ')';
}

double f(Vector3D v) {
return std::sin(v.x) + std::cos(v.y);
}

return Vector3D(std::cos(v.x), -std::sin(v.y), 0.0);
}

int main() {
Vector3D v(1.0, 1.0, 1.0);

}
`

##### Share on other sites

I am trying to calculate the slope of a point given X,Y coordinates... HTH you help me :)