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 4334764
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:34:43+00:00 2026-05-21T10:34:43+00:00

i want to solve a pde according to this matlab code (part of it):

  • 0

i want to solve a pde according to this matlab code (part of it):

.............
% Evaluate the initial conditions
x = xl : dx : xr;              % generate the grid point
% f(1:J+1) since array index starts from 1
f = sin(pi*x) + sin(2*pi*x);  

% store the solution at all grid points for all time steps
u = zeros(J+1,Nt);   

% Find the approximate solution at each time step
for n = 1:Nt
    t = n*dt;         % current time
    % boundary condition at left side
    gl = sin(pi*xl)*exp(-pi*pi*t)+sin(2*pi*xl)*exp(-4*pi*pi*t);   
    % boundary condition at right side
    gr = sin(pi*xr)*exp(-pi*pi*t)+sin(2*pi*xr)*exp(-4*pi*pi*t);  
    if n==1    % first time step
       for j=2:J    % interior nodes     
       u(j,n) = f(j) + mu*(f(j+1)-2*f(j)+f(j-1));
       end     
       u(1,n) = gl;   % the left-end point
       u(J+1,n) = gr; % the right-end point 
    else 
       for j=2:J    % interior nodes
         u(j,n)=u(j,n-1)+mu*(u(j+1,n-1)-2*u(j,n-1)+u(j-1,n-1));
       end
       u(1,n) = gl;   % the left-end point
       u(J+1,n) = gr; % the right-end point 
    end
 end

% Plot the results
tt = dt : dt : Nt*dt;
figure(1)
surf(x,tt, u');     % 3-D surface plot  
............ 

I did this in c++ :

double f(double x){

return sin(pi*x) + sin(2.0*pi*x);

}

double gl(double x,double t){

    return sin(pi*x)*exp(-pi*pi*t) +sin(2.0*pi*x)*exp(-4.0*pi*pi*t);
}

double gr(double x,double t){

    return sin(pi*x)*exp(-pi*pi*t) +sin(2.0*pi*x)*exp(-4.0*pi*pi*t);
}

using namespace std;

const int Nt=50;  // number of time steps
const int J=10; // number of division for x
double xl=0,xr=1.0; //left and right boundaries
double tf=0.1; //final simulation time
double dt,dx; //dx is the mesh size
double x;
double u[J][Nt];


int main()
{
dx=(xr-xl)/J;
dt=tf/Nt;
double m=dt/(dx*dx);


//Evaluate the initial conditions
//generate the grid point
for (x=xl;x<=xr;x+=dx) {
    f(x);
}

//store the solution at all grid points for all time steps
for (int i=0;i<J;i++){
    for (int j=0;j<Nt-1;j++){
        u[i][j]=0;
    }
}

//find the approximate solution at each time step
int n,t,j;
for (n=0;n<Nt;n++){
    t=(n+1)*dt; //current time
        // boundary condition at left side
        gl(xl,t);
        //boundary condition at right side
        gr(xr,t);

        if (n==0) {//first time step
            for (j=1;j<J-1;j++){
                u[j][n]=f(j)+m*(f(j+1)-2.0*f(j)+f(j-1));
                }
        u[0][n]=gl(xl,t);
        u[J-1][n]=gr(xr,t); }
        else  {
            for(j=1;j<J-1;j++){
                u[j][n]=u[j][n-1]+m*(u[j+1][n-1]-2.0*u[j][n-1]+u[j-1][n-1]);
                }
        u[0][n]=gl(xl,t);
        u[J-1][n]=gr(xr,t);
        }

    }

ofstream data;
data.open("Data.dat");


for (int tt=dt;tt<=Nt*dt;tt+=dt){
    data<< tt <<"\t"<<x<<"\t"<< u[J][Nt]<<endl;

}

cout <<"\nFiles written! "<<endl;

    return 0;
}

The problem is that i get a data file with only values 0 1.1 -3.58979e-09.
Maybe the problem is in the indexing?Or i messed up completely?

  • 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-05-21T10:34:43+00:00Added an answer on May 21, 2026 at 10:34 am

    You have several serious problems here:

    1. You have problems with all for lops. You always takes one value more than exist in arrays. Since you start your loops with zero you should use < instead of <=.

    2. You use invalid indices in arrays: u[J][Nt-1] instead of u[i][j] in the initialization loop, and u[J][n] instead of u[J-1][n] in the main loop. For example the initialization loop should be:

      for (int i = 0; i < J; i++) {
          for (int j = 0; j < Nt-1; j++) {
              u[i][j] = 0;
          }
      }
      

      It’s strange that you don’t get access violation exception, since you are trying to access values outside the array.

    3. You check n == 1 instead of n == 0 for the first iteration

    4. For the calculation to be equal with Matlab you should calculate t = (n + 1) * dt;, since in the Matlab you n starts from 1.

    5. Instead the loop for(j=0;j<=J-2;j++) there should be for(j=1; j < J-1; j++)

    6. The f(x) in the first loop has no sense, just like the gl(xl, t) & gr(xr, t) in the beginning of the main loop.

    By the way how have you defined these functions? There also could be problems.

    Meanwhile you should fix these issues before you can continue testing your application.

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

Sidebar

Related Questions

here i want to solve stack overflow issue in this code. here in this
I want to solve this issue in query @ for Ex : id ip
I want to solve an algorithm in which I have this input : n
hiere is my code, within a loop. I want to solve my closure problem.
I want to solve this puzzle but the file resides on remote server. How
I have a little problem: I want to solve this problem with OCaml, so
I've got this equation that I want to solve with a java application. It
I am new to prolog and i want to solve this problem. Suppose I
i want to solve this equation... | 1 1 1 | |b0| |exp(t) |
I know this is a quite easy problem but I just want to solve

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.