easy way to do it?

Hey

I was discusing Maple and Matlab with one of my friend.

He said that in Matlab he can do

a:=1:12

b:=sin(a)

plot(a,b)

that's easy in matlab to do so

i use

> ptlist:=NULL:
> for n to 12 do
> ptlist:=ptlist,[n,sin(n)]:
> od:
> plot([ptlist]);

do to so

 

is there any easy way to do that in maple?

like creat a sequence from 1 to 12? ( besides seq(n,n=1..12))

 

thanks

 

 

John Fredsted's picture

Using seq, after all

Try the following:

plot([seq([i,sin(i)],i = 1..12)]);

PS: It does use seq, which you seem to exclude, but it is shorter and faster than your for-loop construction.

Keeping it Linear

While probably not of immediate interest to the original poster, the reason the loop can be much slower is not so much because it is an external loop, but rather because it is being used to build up a sequence term by term.  Each intermediate term is stored in Maple's simplification table, so, after n loops, it looks like

term1
term1, term2
term1, term2, term3, ..., termn

This is an O(n2) process.  By inserting the terms into a table in the loop, and then converting to a list at the end, this becomes O(n).  Conceptually,

term := table()
for i to n do
   term[i] := (compute the term);
end do;
convert(term, 'list');

Of course, if one can generate the list with one call to seq, that is usually better, however, sometimes it is more convenient to use the imperative style and write a loop.

Robert Israel's picture

Another way

I too would favour the solution using seq, but you could also do something like this:

> plots[listplot](map(sin, [$1..12]));

 

 

the obvious solution

Actually, what we would really do is simply

plot(sin, 1..12);

which is shorter and nicer than what is required in Matlab.  No use plotting vectors/lists if you can use Maple's adapative plotting capability. Just because Matlab is restricted to plotting vectors doesn't mean that that is the best way to go.

 

John Fredsted's picture

Intention

But is that what the author was asking for? Your suggestion produces, of course, a smooth plot, while his produces a piecewise linear plot.

i think all of you got a

i think all of you got a nice idea of doing it

however

i think may be what my friend what to do with maple is :

suppose he has got several set of data
a:=set of data
b:=sin(set of data)
c:=cos(set of data)
3d plot
is there any simple way to do so?

not like smooth plot liek plot( sin,1..12), thanks anyway

======================

For Maple on MAC
how do i open classical worksheet if it is not on the desktop?

John Fredsted's picture

An attempt

I would do something like

s := {seq}(i,i = 1..5000):
plots[pointplot3d](map(x -> [x,sin(x),cos(x)],s));

which produces the following plot:

1808_pointplot3d.gif

yep thanks "map" is always

yep
thanks
"map" is always very handy though i didnt use it very often. I will try to use that more often

=============
For Maple on MAC
how do i open classical worksheet if it is not on the desktop?

Robert Israel's picture

Mac

There is no Classic interface for the Mac.  You can, of course, open a Classic worksheet (.mws file) with the Standard interface.

tweaks

Hi John.  A minor "improvement" is to use the one-argument form of seq:

  s := {seq(1..5000)};

It is unfortunate that Maple does not have a builtin identity function (I've lobbied for `()`, as it is analagous to `[]` and `{}`).   If it did you could do

  plots[pointplot3d](map([`()`,sin,cos], s);

This can be done with the user-defined one-argument identity

  plots[pointplot3d](map([x->x,sin,cos], s)

but I prefer the look of an argumentless operator.  By the way, here is the first assignment using `{}`:

  s := `{}`(seq)(1..5000);

 

John Fredsted's picture

I somehow expected it :-)

I was working in Maple 9.5, where

s := {seq(1..5000)};

is not possible, and I did not bother to fire up Maple 11, to ascertain the shorter syntax, so I just decided to write it using the old syntax even though I knew that it probably would be commented on. I apologize for that inconvenience.

I like your idea, by itself, of using () as the identity map, but also because I like the simplicity of your expression

plots[pointplot3d](map([`()`,sin,cos], s);
Doug Meade's picture

Another approach

I agree that it is not always appropriate to try to use the same approach to a problem in different software systems. Each system has its own strengths and these need to be taken into account. The different approaches suggested for this problem are all reasonable.

But, nobody has yet presented what is the most appropriate way to approach this problem.

s := [$1..5000]:
plots[pointplot3d]([seq([x,sin(x),cos(x)],x=s)]);

Note that Maple makes a distinction between a set and a list. In most cases like this, a list (with order) is what users want. Sets (without order) do have their uses, but you have to be careful to select the right data structure for the right problem.

To close, I would note that I would create this specific plot without explicitly constructing the data:

plots[pointplot3d]([seq([x,sin(x),cos(x)],x=[$1..5000]);

I hope this is helpful,

Doug

---------------------------------------------------------------------
Douglas B. Meade
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu/~meade/

$ and seq

This particular procedure, pointplot3d, can take either a set or a list.  While it doesn't matter here, because all points are distinct, it is conceivable that a set could be an advantage because it would eliminate redundant points. However, I suspect pointplot3d converts to a set anyway.

While the $ operator is more compact, the seq function is generally preferred.  It is more efficient (doesn't matter here).  Actually, there isn't any reason to use the $ in your example, since you already call seq (see below).  The reason, I assume, that John proposed a separate call to seq was to explicitly assign/show the generation of the domain.

plots[pointplot3d]([seq([x,sin(x),cos(x)],x=1..5000)]);
John Fredsted's picture

Correctly assumed

With regard to the separate call to seq, you assumed correctly. I was not overly concerned with making the code as short as possible, more with making it transparent.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}