GithubHelp home page GithubHelp logo

funtions with multiple arguments about findiff HOT 3 CLOSED

maroba avatar maroba commented on August 18, 2024
funtions with multiple arguments

from findiff.

Comments (3)

maroba avatar maroba commented on August 18, 2024

The problem is that you tried to feed the function with two 1D-arrays of coordinate values, but actually you need a 2D grid in your example. You can create the 2D grid arrays from the coordinate arrays using the numpy.meshgrid function:

from findiff import FinDiff
import numpy as np
def f(x, y):
    return 5*x**2 - 5*x + 10*y**2 -10*y

diff = .01
x = np.linspace(-10,0,11)
y = np.linspace(0,10,11)

X, Y = np.meshgrid(x, y, indexing='ij')

d_dx = FinDiff(0, diff)

df_dx = d_dx(f(X, Y))

np.meshgrid works for any number of dimensions.

FinDiff determines the grid dimension by the arrays that you apply it to. If you pass a 1D-array to a FinDiff object, it thinks that your grid is only 1D. Incidentally, if you had calculated the partial derivative with respect to y instead of x in your original implementation, it would have thrown an IndexError exception, because there is no second index in the 1D array. All in all, if you use grid arrays, all should be fine. :-)

from findiff.

RoyLarson avatar RoyLarson commented on August 18, 2024

This is the output I expect when I inputted what I put in.

  array([[-105.],
    [ -95.],
    [ -85.],
    [ -75.],
    [ -65.],
    [ -55.],
    [ -45.],
    [ -35.],
    [ -25.],
    [ -15.],
    [  -5.]])

This is the output that I get when I input what you say to

array([[-10500., -10500., -10500., -10500., -10500., -10500., -10500.,
    -10500., -10500., -10500., -10500.],
    [ -9500.,  -9500.,  -9500.,  -9500.,  -9500.,  -9500.,  -9500.,
    -9500.,  -9500.,  -9500.,  -9500.],
    [ -8500.,  -8500.,  -8500.,  -8500.,  -8500.,  -8500.,  -8500.,
    -8500.,  -8500.,  -8500.,  -8500.],
    [ -7500.,  -7500.,  -7500.,  -7500.,  -7500.,  -7500.,  -7500.,
    -7500.,  -7500.,  -7500.,  -7500.],
    [ -6500.,  -6500.,  -6500.,  -6500.,  -6500.,  -6500.,  -6500.,
    -6500.,  -6500.,  -6500.,  -6500.],
    [ -5500.,  -5500.,  -5500.,  -5500.,  -5500.,  -5500.,  -5500.,
    -5500.,  -5500.,  -5500.,  -5500.],
    [ -4500.,  -4500.,  -4500.,  -4500.,  -4500.,  -4500.,  -4500.,
    -4500.,  -4500.,  -4500.,  -4500.],
    [ -3500.,  -3500.,  -3500.,  -3500.,  -3500.,  -3500.,  -3500.,
    -3500.,  -3500.,  -3500.,  -3500.],
    [ -2500.,  -2500.,  -2500.,  -2500.,  -2500.,  -2500.,  -2500.,
    -2500.,  -2500.,  -2500.,  -2500.],
    [ -1500.,  -1500.,  -1500.,  -1500.,  -1500.,  -1500.,  -1500.,
    -1500.,  -1500.,  -1500.,  -1500.],
    [  -500.,   -500.,   -500.,   -500.,   -500.,   -500.,   -500.,
    -500.,   -500.,   -500.,   -500.]])

The numbers are still off, and there are more of them.

Also how do I get just one point

df_0_0_dx = d_dx(f((0,0)))

If you want to look at the jupyter notebook I am trying to figure this out on it is

https://github.com/RoyLarson/Simple_NG_Piping_Pressure_Drop/blob/initial_commit/tests/findiff_test.ipynb

from findiff.

RoyLarson avatar RoyLarson commented on August 18, 2024

I figured this out.
I thought your program took at function evaluated the function along with the given dx and sent back the result at the point.
It instead evaluates the finite differences for already evaluated points from a function so the grid had to already be computed.

from findiff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.