# Emulate MatLab functions using Wolfram language?

**URL:** <https://community.wolfram.com/t/emulate-matlab-functions-using-wolfram-language/14689>\
**Category:** Questions\
**Tags:** computer-science, external-interfaces\
**Created:** [September 4, 2019, 3:29pm UTC](https://community.wolfram.com/t/emulate-matlab-functions-using-wolfram-language/14689 "2019-09-04T15:29:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![godsaperture](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/godsaperture/32/32164_2.png) [@godsaperture](https://community.wolfram.com/u/godsaperture)\
**Post date:** [September 4, 2019, 3:29pm UTC](https://community.wolfram.com/t/emulate-matlab-functions-using-wolfram-language/14689/1 "2019-09-04T15:29:47Z")

</div>

So, I’m attempting to emulate MatLab functions in Mathematica (mainly for practice in programming) and I have run across a very peculiar error where If doesn’t simplify down to the False portion of the function:

```
Trapz[xlist_: Null, ylist_] := Block[
{lxlist, rxlist, lylist, rylist, ylen, i, k, A, B = {}, listx},
If[Length[Dimensions[ylist]] == 1,

rxlist = 
Prepend[If[xlist == Null, Table[i, {i, 1, Length[ylist]}], xlist],
0];
lxlist = 
Append[If[xlist == Null, Table[i, {i, 1, Length[ylist]}], xlist], 
0];
lylist = Prepend[ylist, 0];
rylist = Append[ylist, 0];
	Delete[
If[xlist == Null, Table[1, {i, 1, Length[ylist]}], 
rxlist - lxlist], {{1}, {-1}}].
	Delete[(lylist + rylist)/2, {{1}, {-1}}]
,
ylen = Length[ylist];

If[xlist == Null, listx = Table[1, {i, 1, Length[ylist[[1]]]}],
listx = xlist]; (*Error begins here and continues onward*)

	For[i = 1, i <= ylen, i++,
lxlist = Prepend[listx, 0];
rxlist = Append[listx, 0];
lylist = Prepend[ylist[[i]], 0];
rylist = Append[ylist[[i]], 0];
For[i = 1, i <= ylen, i++, A = Append[B, ylist[[i]].listx]; 
B = A]; B
]]]

```

After evaluating this code:

```
Trapz[Table[i, {i, 0, 5}], Table[i^2, {i, 0, 5}]]

```

I receive this:

```
If[{1, 1, 1, 1, 1, 1}].{1/2, 5/2, 13/2, 25/2, 41/2}

```

And an error that sta

[Untitled-1.nb](https://community.wolfram.com/uploads/short-url/osoJC18Rtf32SeKoEmbaXyEfuag.nb) (8.02 KB)

---

<div class="post-metadata">

**Author:** ![neils](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/neils/32/20595_2.png) [@neils](https://community.wolfram.com/u/neils)\
**Post date:** [September 4, 2019, 5:50pm UTC](https://community.wolfram.com/t/emulate-matlab-functions-using-wolfram-language/14689/2 "2019-09-04T17:50:11Z")

</div>

Joshua,

Use SameQ (===) instead of Equal in If. Mathematica can always tell if two items are not the same but Equal is more difficult (read the documentation  it describes this in detail). For example, q is not the same as y but they might be Equal.

Regards

Neil

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [September 4, 2019, 5:52pm UTC](https://community.wolfram.com/t/emulate-matlab-functions-using-wolfram-language/14689/3 "2019-09-04T17:52:31Z")

</div>

Hi Joshua,

At least one issue with the code is the `If` condition `xlist == Null`. It will remain unevaluated unless `xlist` is Null. So the `If` will remain unevaluated.

```
{0, 1, 2, 3, 4, 5} == Null
(* {0, 1, 2, 3, 4, 5} == Null *)

If[{0, 1, 2, 3, 4, 5} == Null, True, False]
(* If[{0, 1, 2, 3, 4, 5} == Null, True, False] *)

```

Rather than testing for `Null`, test the `Length` of the list.
