Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.4K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

Shortcut if conditions similar to matlab

Posted 5 years ago

Dear Friends,

I would like to know the short cut for the if conditions on the picture attached. Top one is what I want and the bottom one is the Matlab code.

I do not want to hard code it in Mathematica by using IF[ ];

Thank you.

Attachment

Attachments:
POSTED BY: Sheng Dai
2 Replies
Posted 5 years ago

Hi Sheng,

As Hans mentioned, you can use Piecewise to do this

s3[t_] := Piecewise[{{(A t)/4, 0 <= t < 2.5}, {(-A t + 5 A)/4, 2.5 <= t < 5}}]
Block[{A = 1}, Plot[s3[t], {t, 0, 6}]]

You can also use DownValues, which is closer to the Matlab code

s3a[t_ /; 0 <= t < 2.5] := (A t)/4
s3a[t_ /; 2.5 <= t < 5] := (-A t + 5 A)/4
s3a[___] := 0

Block[{A = 1}, Plot[s3a[t], {t, 0, 6}]]
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Try Piecewise

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