Message Boards Message Boards

0
|
2258 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Why does Mathematica object when I use y[x_,t_] but not when I use just y?

Posted 1 year ago

I'm defining my function y, which is a function of x and t, below, as just y. Mathematica likes that fine, and prints graphs as I want. Below is the code Im using, which Mathematica is fine with:

ClearSystemCache[]
h=1; l=10; v=1;
y=Sum[(8h/(n^2Pi^2))(2Sin[n Pi/4]-Sin[n Pi/2])Sin[n Pi x/l]Cos[n Pi v*t/l],{n,1,40,1}];
y2=Table[Plot[y,{x,0,10}, AxesLabel->{"x","y[x,t]"}],{t,0,2l/v,.02l/v}]; ListAnimate[y2] 

However, the following code, which just uses y[x,t] in place of y in the definition for y, which should be okay, produces an error msg:

ClearSystemCache[]

    h=1; l=10; v=1;

    y[x_,t_]=Sum[(8h/(n^2Pi^2))(2Sin[n Pi/4]-Sin[n Pi/2])Sin[n Pi x/l]Cos[n Pi v*t/l],{n,1,40,1}];

    y2=Table[Plot[y,{x,0,10}, AxesLabel->{"x","y[x,t]"}],{t,0,2l/v,.02l/v}];

    ListAnimate[y2] 

Why is this?

POSTED BY: Lewis Jones
2 Replies
Posted 1 year ago

Couple of things going on. ClearSystemCache doesn't clear definitions. ClearAll would be the typical thing to do here, as Henrik pointed out. You should also familiarize yourself with Clear, Unset, Remove and others that you'll find in the documentation.

So, in your second attampt, y still had a value assigned to it when you tried to define y[x_, t_]. Let's look at a simpler example:

y=7;
y[x_] := 7 x;

You'll get a message like this: SetDelayed::write: Tag Integer in 7[x_] is Protected.

The integer it's referring to is the 7. Notice that what it was trying to define was actually 7[x_]. To evaluate y[x_], it needs to first evaluate y, which was 7. Integers have the Protected attribute, which prevents creating new definitions.

But even if you use ClearAll before your second attempt, you're still going to have problems. In this expression,

Plot[y, {x, 0, 10}, AxesLabel -> {"x", "y[x,t]"}]

the y will now have DownValues, but not OwnValues. So, the y as an expression won't evaluate to anything else, and Plot will produce an empty plot in that case. You're expecting this to plot the function that you are associating to y in your head, but it's just plotting a literal symbol y (and Plot is designed to produce an empty plot in that case).

So, you need to tell Plot the full expression that you want it to work with:

Plot[y[x, t], {x, 0, 10}, AxesLabel -> {"x", "y[x,t]"}]

(Of course, this still needs to be inside the Table so that t gets replaced with actual values.)

POSTED BY: Eric Rimbey

Instead of ClearSystemCache[] use ClearAll[y]

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract