Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:27:36+00:00 2026-06-14T04:27:36+00:00

in order to code the DEL2 matlab function in c++ I need to understand

  • 0

in order to code the DEL2 matlab function in c++ I need to understand the algorithm. I’ve managed to code the function for elements of the matrix that are not on the borders or the edges.
I’ve seen several topics about it and read the MATLAB code by typing “edit del2” or “type del2” but I don’t understand the calculations that are made to obtain the borders and the edges.

Any help would be appreciated, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:27:37+00:00Added an answer on June 14, 2026 at 4:27 am

    You want to approximate u” knowing only the value of u on the right (or the left) of a point.
    In order to have a second order approximation, you need 3 equations (basic taylor expansion):

    u(i+1) = u(i) + h u’ + (1/2) h^2 u” + (1/6) h^3 u”’ + O(h^4)

    u(i+2) = u(i) + 2 h u’ + (4/2) h^2 u” + (8/6) h^3 u”’ + O(h^4)

    u(i+3) = u(i) + 3 h u’ + (9/2) h^2 u” + (27/6) h^3 u”’ + O(h^4)

    Solving for u” gives (1):

    h^2 u” = -5 u(i+1) + 4 u(i+2) – u(i+3) + 2 u(i) +O(h^4)

    To get the laplacian you need to replace the traditional formula with this one on the borders.

    For example where “i = 0” you’ll have:

    del2(u) (i=0,j) = [-5 u(i+1,j) + 4 u(i+2,j) – u(i+3,j) + 2 u(i,j) + u(i,j+1) + u(i,j-1) – 2u(i,j) ]/h^2

    EDIT clarifications:

    The laplacian is the sum of the 2nd derivatives in the x and in the y directions. You can calculate the second derivative with the formula (2)

    u” = (u(i+1) + u(i-1) – 2u(i))/h^2

    if you have both u(i+1) and u(i-1). If i=0 or i=imax you can use the first formula I wrote to compute the derivatives (notice that due to the simmetry of the 2nd derivative, if i = imax you can just replace “i+k” with “i-k”). The same applies for the y (j) direction:

    On the edges you can mix up the formulas (1) and (2):

    del2(u) (i=imax,j) = [-5 u(i-1,j) + 4 u(i-2,j) – u(i-3,j) + 2 u(i,j) + u(i,j+1) + u(i,j-1) – 2u(i,j) ]/h^2

    del2(u) (i,j=0) = [-5 u(i,j+1) + 4 u(i,j+2) – u(i,j+3) + 2 u(i,j) + u(i+1,j) + u(i-1,j) – 2u(i,j) ]/h^2

    del2(u) (i,j=jmax) = [-5 u(i,j-1) + 4 u(i,j-2) – u(i,j-3) + 2 u(i,j) + u(i+1,j) + u(i-1,j) – 2u(i,j) ]/h^2

    And on the corners you’ll just use (1) two times for both directions.

    del2(u) (i=0,j=0) = [-5 u(i,j+1) + 4 u(i,j+2) – u(i,j+3) + 2 u(i,j) + -5 u(i,j+1) + 4 u(i+2,j) – u(i+3,j) + 2 u(i,j)]/h^2

    Del2 is the 2nd order discrete laplacian, i.e. it permits to approximate the laplacian of a real continuous function given its values on a square cartesian grid NxN where the distance between two adjacent nodes is h.

    h^2 is just a constant dimensional-factor, you can get the matlab implementation from these formulas by setting h^2 = 4.

    For example, if you want to compute the real laplacian of u(x,y) on the (0,L) x (0,L) square, what you do is writing down the values of this function on an NxN cartesian grid, i.e. you calculate u(0,0), u(L/(N-1),0), u(2L/(N-1),0) … u( (N-1)L/(N-1) =L,0) … u(0,L/(N-1)), u(L/(N-1),L/(N-1)) etc. and you put down these N^2 values in a matrix A.

    Then you’ll have
    ans = 4*del2(A)/h^2, where h = L/(N-1).

    del2 will return the exact value of the continuous laplacian if your starting function is linear or quadratic (x^2+y^2 fine, x^3 + y^3 not fine). If the function is not linear nor quadratic, the result will be more accurate the more points you use (i.e. in the limit h -> 0)

    I hope this is more clear, notice that i used 0-based indices for accessing matrix (C/C++ array style), while matlab uses 1-based.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our application is using initialization code that depends on the order static code is
How to display a table order by code (like 01, 02… then null columns)?
I am using .htaccess code in order to pass pages titles to the url
I use Acceleo in order to generate code with a model I have made.
I got this code in order to build an url for the link using
I'm having troubles with the order of my code in my program. right now
I am using the below JS code in order to change the class when
I've been using the below code in order to get the Windows License Key.
I am trying to create a table within PHP code in order to open
In a BlackBerry application, I am using this code in order to get a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.