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
Attachments: