# Help with this formula...

This topic is 1845 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

Thanks for the reply.

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?

[code]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[/code]

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);
}

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

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

std::cout << Gradient(v) << std::endl;
}
`

##### Share on other sites

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

##### Share on other sites
What is the function that gives me the height of a given point (x,y)?

##### Share on other sites

http://magician.ucsd.edu/essentials/webbookse112.html

look at the gradient section A.6

Thanks

Edited by MARS_999

##### Share on other sites

I understand that section. It still doesn't tell me what you are trying to do.

##### Share on other sites

find the slope of a point. Used for the normal on a 2d heightmap for terrain....

##### Share on other sites

There is no such thing as the slope of a point. There is a notion of the slope of a function along a direction, formally known as the directional derivative. Among the directional derivatives corresponding to directions with length 1, the maximum is achieved in the direction of the gradient.

So, do you want to compute the direction in which the function grows fastest (the gradient), or how fast it grows in that direction (the directional derivative in the direction of the gradient), or something else?

##### Share on other sites

The former I assume?

Normals are -1 to 1 and wouldn't that be a derivative? So if you could please show both forms and I can try them both and see what the results are... and see if I like the results it shows?

Thanks

##### Share on other sites

I am not done asking questions. :)

So you have a terrain described as a 2D heightmap. I assume that means you know the elevation of the terrain at the points of a grid. However, normals, gradients and all these things are only defined if you know the elevation of any point, not just the ones in the grid. You could use an interpolation method to extend the function to non-grid points in a reasonable manner, but perhaps this is beyond what you intend to do.

Alternatively, you can use discreet approximations to these things. If you do that, the gradient at (x,y) is simply the vector (height(x+1,y)-height(x,y), height(x,y+1)-height(x,y)).

Are we getting closer?