Jump to content

  • Log In with Google      Sign In   
  • Create Account

Help with this formula...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 28 December 2012 - 09:19 PM

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 70229138.png

 

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



Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 28 December 2012 - 09:43 PM

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

 

z.png

gradient.png

 

Then:

 

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, 28 December 2012 - 09:46 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 28 December 2012 - 09:47 PM

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

 

post-67860-0-55136700-1356752592.png

 

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

 

Thanks!


Edited by MARS_999, 28 December 2012 - 09:52 PM.


#4 Álvaro   Crossbones+   -  Reputation: 13897

Like
0Likes
Like

Posted 28 December 2012 - 11:18 PM

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.

#5 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 28 December 2012 - 11:37 PM

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)?



#6 Álvaro   Crossbones+   -  Reputation: 13897

Like
0Likes
Like

Posted 29 December 2012 - 12:03 AM

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.

#7 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 29 December 2012 - 08:55 AM

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?



#8 Álvaro   Crossbones+   -  Reputation: 13897

Like
0Likes
Like

Posted 29 December 2012 - 09:17 AM

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


#9 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 29 December 2012 - 09:37 AM

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



#10 Álvaro   Crossbones+   -  Reputation: 13897

Like
0Likes
Like

Posted 29 December 2012 - 09:38 AM

What is the function that gives me the height of a given point (x,y)?

#11 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 29 December 2012 - 09:41 AM

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

 

look at the gradient section A.6

 

Thanks


Edited by MARS_999, 29 December 2012 - 09:41 AM.


#12 Álvaro   Crossbones+   -  Reputation: 13897

Like
0Likes
Like

Posted 29 December 2012 - 09:43 AM

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



#13 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 29 December 2012 - 09:51 AM

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



#14 Álvaro   Crossbones+   -  Reputation: 13897

Like
1Likes
Like

Posted 29 December 2012 - 09:59 AM

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?



#15 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 29 December 2012 - 10:08 AM

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



#16 Álvaro   Crossbones+   -  Reputation: 13897

Like
0Likes
Like

Posted 29 December 2012 - 10:15 AM

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?



#17 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 29 December 2012 - 10:24 AM

If I have a grid say 64x64 then 0-63 in the x direction and 0-63 in the y direction, with each grid point holds the height(elevation) yes I know that data. That point could be anywhere from 0 to skys the limit.... :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS