Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I was trying to plot the following:

j = 10; 
a = 0; b = 0; s = 0; 

B[n_] = Integrate[2*Sin[n*Pi*x]*x, {x, 0, 1}];
u[x_, t_] = Sum[B[n]*Sin[n*Pi*x]*Exp[-(n*Pi)^2*t], {n, 1, j}];

K[x_, t_] = (1/(2*Pi))*
   Integrate[
    Exp[I*x*psi]*(Exp[-I*b*psi]*Exp[-I*a*psi^2]*
        Exp[-I*t*psi^2])/((1 + psi^2)^s), {psi, -10, 10}];

S = (Total[u[x, t]]/Length[u[x, t]]);

T[x_, t_] = Integrate[K[x - y, t]*y*S, {y, -10, 10}]

Plot3D[T[x, t], {x, 0, 1}, {t, 0.01, 1}, AxesLabel -> {"x", "t", "y"}, Boxed -> False, Mesh -> False]

The problem is that this is a really complicated solution and I would like to get the results numerically, but it ran for a long time and nothing plotted -- just empty axis. In my definition of all the variables -- I have also used the := statement, but it didn't help!

If anyone can please help me with this then I'll greatly appreciate it.

Thanks!

share|improve this question
    
Are you sure T[x,t] is a real number ? –  b.gatessucks Jul 12 '12 at 20:10
    
it is supposed to be based on the definition of this formula! –  user1482746 Jul 12 '12 at 20:25
add comment

migrated from stackoverflow.com Jul 12 '12 at 19:42

This question came from our site for professional and enthusiast programmers.

2 Answers

I changed things to

j = 10;
a = 0; b = 0; s = 0;

B[n_] = Integrate[2*Sin[n*Pi*x]*x, {x, 0, 1}];

u[x_, t_] = Sum[
  B[n]*Sin[n*Pi*x]*Exp[-(n*Pi)^2*t],
  {n, 1, j}
  ];

K[x_?NumericQ, t_] = (1/(2*Pi))*
  Integrate[
   Exp[I*x*psi]*(Exp[-I*b*psi]*Exp[-I*a*psi^2]*
       Exp[-I*t*psi^2])/((1 + psi^2)^s), {psi, -10, 10}];

S[x_, t_] := (Total[u[x, t]]/Length[u[x, t]]);

T[x_, t_] := NIntegrate[K[x - y, t]*y*S[x, t], {y, -10, 10}]

and now T[.3,.4] results in a message Nonatomic expression expected at position 1 in Total[0.0193712]. So there is something wrong with S since u returns a number, but there is a Total[u[x,t]].

Maybe I messed it up; if you are following the question please give some feedback (I am asking because this has been migrated from SO).

share|improve this answer
    
Hi...Many thanks for your help....I tried to run it and got thr same Nonatomic expression stuff! I am not really a big time programmer so I am not sure, but the S is just supposed to be an average of "u" and is to be used in the convolution in T....Can you please help me to make this run...I'll really appreciate it! Please do tell me if I can do anything more from my side –  user1482746 Jul 12 '12 at 20:13
    
@user1482746 the problem is that u is a Sum so basically it returns a number. Could you explain what you mean by average of u? is u supposed to be a list, instead of a sum as it currently is? –  acl Jul 12 '12 at 20:15
    
since "u" depends on x and t ...it returns numbers for each (x,t) so basically it is a list as I am considering it...but "u" as defined in the 4th line is from a formula..I want to use the average of all those u's in the convolution in T....the S works with another K --- if you can kindly look at stackoverflow.com/questions/11434828/… –  user1482746 Jul 12 '12 at 20:21
    
it works for that case....but I don't know why it doesn't work with this K...that's my problem! –  user1482746 Jul 12 '12 at 20:22
    
@user1482746 no, look, the way it's written, u is a number, not a list. For example, Sum[i,{i,1,10}] sums the integers from 1 to 10 and returns the answer, 55. If you wanted a list, you do not want Sum but Table. Perhaps you could explain what u is supposed to return for u[.1,.2]? –  acl Jul 12 '12 at 20:29
show 3 more comments

A couple of comments:

  • You will probably find it very useful to read about the difference between = and :=; this is extremely important in Mathematica, so much so that you will probably not be able to write code on your own until you understand them.
  • I'm not sure exactly what problem you're trying to solve with your code, but if it involves a convolution you should really look at Convolve or ListConvolve - these will be much more efficient and the code will be easier to write and understand.
  • It looks like you're trying to treat u[x,t] as a vector, but based on the definition it's just a number, not a vector - I don't think you need S at all.

Based on these I changed some things as shown below.

j = 10;
a = 0; b = 0; s = 0;

ClearAll[B, u, K, S, T];
B[n_] := Integrate[2*Sin[n*Pi*q]*q, {q, 0, 1}];
u[x_, t_] := Sum[B[n]*Sin[n*Pi*x]*Exp[-(n*Pi)^2*t], {n, 1, j}];

K[x_, t_] := (1/(2*Pi))*
   Integrate[
    Exp[I*x*psi]*(Exp[-I*b*psi]*Exp[-I*a*psi^2]*
        Exp[-I*t*psi^2])/((1 + psi^2)^s), {psi, -10, 10}];

T[x_, t_] := NIntegrate[K[x - y, t]*y*u[x, t], {y, -10, 10}]

It is now possible to evaluate T, but it comes out to a complex number. Using Plot3D therefore seems inappropriate.

share|improve this answer
    
thanks again for your help! suppose if I change the definition of S[x_, t_] := (Total[u[x/2, t]]/Length[u[x/2, t]]); then can you please tell me if it can still work? I need to have to S in there for my task to be met. Also, can you please tell me how can I modify the Plot3D so that I can extract the real parts? –  user1482746 Jul 12 '12 at 20:44
    
I think the problem is that S (which you got rid of) is meant to be some kind of average, but as it is written it's a number. Of course one solution is to just replace S by u, as you did :) But I think this question is based on some misunderstanding on how Plot3D works (ie, it seems userxxxxx thinks this works like matlab; but I'm not sure) –  acl Jul 12 '12 at 20:46
    
@acl I agree; there is definitely a confusion between functions and vectors in the code, and I certainly don't understand what userxxxx is trying to do. –  DGrady Jul 12 '12 at 20:50
    
@user1482746 You're welcome, but I don't think I can provide much more help without understanding what problem you're trying to solve. –  DGrady Jul 12 '12 at 20:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.