General report
How to solve Basic Engineering and Mathematics Problems
using Mathematica, Matlab and Maple
April 22, 2021 Compiled on April 22, 2021 at 4:04am
This is a collection of how to examples showing the use of Mathematica and Matlab to solve basic engineering and mathematics problems. Few examples are also in Maple, Ada, and Fortran.
This was started as a cheat sheet few years ago, and I continue to update it all the time.
Few of the Matlab examples require the use of toolboxs such as signal processing toolbox and the control systems toolbox (these come free as part of the student version). Most examples require only the basic system installation.
Problem: Find the transfer function \(H(s)\) given zeros \(s=1,s=2\), poles \(s=0,s=4,s=6\) and gain 5.

\[ \text {tf} = \left [ {\begin {array}{c} {\frac {5\,{s}^{2}+15\,s+10}{{s}^{3}+10\,{s}^{2}+24\,s}}\end {array}} \right ] \] 

\[ 5*s^2+15*s+10 \] 

\[ s*(s^2+10*s+24) \] 
Problem: Find the state space representation for the continuous time system deﬁned by the transfer function \[ H(s)=\frac {5s}{s^{2}+4s+25}\]





\[ \left [{\begin {array}{cc} 0 & 1 \\ 25 & 4 \end {array}} \right ] \] 

\[ \left [ {\begin {array}{c} 0\\ 1\end {array}} \right ] \] 
 \[ \left [ {\begin {array}{cc} 0&5\end {array}} \right ] \] 

\[ \left [ {\begin {array}{c} 0\end {array}} \right ] \] 
Problem: Given the continuous time S transfer function deﬁned by \[ H(s)=\frac {1+s}{s^{2}+s+1}\] convert to state space and back to transfer function.
Mathematica

\[ \left ( {\begin {array}{cccc} 0 & 1 & 0 \\ 1 & 1 & 1 \\ \hline 1 & 1 & 0 \\ \end {array}} \right )_{} \] 

\[ \frac {s+1}{s^2+s+1} \] 
Matlab




Problem: Find the state space representation and the step response of the continuous time system deﬁned by the Matrices A,B,C,D as shown
Mathematica


Matlab


Maple


Problem: Compute the gain and phase margins of the openloop discrete linear time system sampled from the continuous time S transfer function deﬁned by \[ H(s)=\frac {1+s}{s^{2}+s+1}\] Use sampling period of 0.1 seconds.
Mathematica








Matlab




Problem: Obtain the transfer and state space representation for the diﬀerential equation \[ 3\frac {d^{2}y}{dt^{2}}+2\frac {dy}{dt}+y\relax (t) = u(t) \]
Mathematica




Matlab




Maple

\[ \frac {1}{3{s}^{2}+2\,s+1} \] 

\[ \left \{ \left [ {\begin {array}{cc} 0&1\\ \noalign {\medskip }1/3&2/3 \end {array}} \right ] , \left [ {\begin {array}{c} 0\\ \noalign {\medskip } 1\end {array}} \right ] , \left [ {\begin {array}{cc} 1/3&0\end {array}} \right ] , \left [ {\begin {array}{c} 0\end {array}} \right ] \right \} \] 
Problem: Convert \[ H\relax (s) = \frac {1}{s^2+10 s+10} \]
To \(Z\) domain transfer function using sampling period of \(0.01\) seconds and using the zero order hold method.
Mathematica


Matlab


Problem: Give a continuous time transfer function, show how to convert it to an ordinary diﬀerential equation. This method works for nondelay systems. The transfer function must be ratio of polynomials. For additional methods see this question at stackexchange
Mathematica


Problem: Find the unit step response for the continuous time system deﬁned by the transfer function \[ H(s) = \frac {25}{s^{2}+4s+25} \]
Mathematica


Matlab


Maple


Problem: Find the impulse and step responses for the continuous time system deﬁned by the transfer function \[ H\relax (s) =\frac {1}{s^{2}+0.2s+1}\] and display these on the same plot up to some \(t\) value.
Side note: It was easier to see the analytical form of the responses in Mathematica and Maple so it is given below the plot.
Mathematica






Matlab


Maple Using Maple DynamicSystems


Using Laplace transform method:


Problem: Find the response for the continuous time system deﬁned by the transfer function \[ H(s)=\frac {1}{s^{2}+0.2s+1}\] when the input is given by \[ u(t)=\sin (t)\] and display the response and input on the same plot.
Side note: It was easier to see the analytical form of the responses in Mathematica and Maple so it is given below the plot.
Mathematica


Matlab


Maple


Using DynamicSystem package


Problem: Find the zeros, poles, and gain for the continuous time system deﬁned by the transfer function \[ H(s)=\frac {25}{s^{2}+4s+25}\]
Mathematica




Matlab


Maple


Problem: Generate a Bode plot for the continuous time system deﬁned by the transfer function \[ H(s)=\frac {5s}{s^{2}+4s+25}\]
Mathematica


Matlab


Maple or can plot the the two bode ﬁgures on top of each others as follows


A system described by \begin {align*} x' &= Ax+Bu \\ y &= Cx+Du \end {align*}
Is controllable if for any initial state \(x_0\) and any ﬁnal state \(x_f\) there exist an input \(u\) which moves the system from \(x_0\) to \(x_f\) in ﬁnite time. Only the matrix \(A\) and \(B\) are needed to decide on controllability. If the rank of \[ [B \> AB\> A^2B\> \ldots \> A^{n1}B] \] is \(n\) which is the number of states, then the system is controllable. Given the matrix \[ A=\left ({\begin {array}{cccc} 0&1&0&0\\ 0&0&1&0\\ 0&0&0&1\\ 0&0&5&0 \end {array}} \right ) \] And \[ B=\left ({\begin {array}{c} 0\\ 1\\ 0\\ 2 \end {array}} \right ) \]
Mathematica

\[ \left ( {\begin {array}{cccc} 0 & 1 & 0 & 2 \\ 1 & 0 & 2 & 0 \\ 0 & 2 & 0 & 10 \\ 2 & 0 & 10 & 0 \\ \end {array}} \right ) \] 
 True 

4 
Matlab



4 
Maple

\[ \left [ {\begin {array}{cccc} 0&1&0&2\\ \noalign {\medskip }1&0&2&0 \\ \noalign {\medskip }0&2&0&10\\ \noalign {\medskip }2&0&10&0 \end {array}} \right ] \] 

true 

true 

4 
Problem: Given the continuous time S transfer function deﬁned by \[ H(s)=\frac {s^{4}+8s^{3}+16s^{2}+9s+9}{s^{3}+6s^{2}+11s+6}\] obtain the partialfractions decomposition.
Comment: Mathematica result is easier to see visually since the partialfraction decomposition returned in a symbolic form.
Mathematica


Matlab


Maple

\[ s+2 \frac {7}{s+2}{\frac {9}{2\,s+6}}+{\frac {9}{2\,s+2 }} \] 

\[ [s,2,\frac {7}{s+2},{\frac {9}{2\,s+6}},{\frac {9}{2\,s+2}}] \] 
Problem: Obtain the Laplace transform for the function deﬁned in the following ﬁgure.
Function f(t) to obtain its Laplace transform
Comment: Mathematica solution was easier than Matlab’s. In Matlab the deﬁnition of the Laplace transform is applied to each piece separately and the result added. Not ﬁnding the piecewise maple function to access from inside MATLAB did not help.
Mathematica


Matlab


Maple With Maple, had to use Heaviside else Laplace will not obtain the transform of a piecewise function.

\[ {\frac {1{{e}^{sT}}}{{s}^{2}}} \] 
Problem: Obtain the inverse Laplace transform for the function \[ H(s)=\frac {s^{4}+5s^{3}+6s^{2}+9s+30}{s^{4}+6s^{3}+21s^{2}+46s+30}\]
Mathematica
\[ \delta (t)+\left (\frac {1}{234}+\frac {i}{234}\right ) e^{(13 i) t} \left ((73+326 i) e^{6 i t}+(32673 i)\right )\frac {3 e^{3 t}}{26}+\frac {23 e^{t}}{18} \]
Matlab
Maple
\[ Dirac \relax (t ) {\frac {3\,{{\rm e}^{3\,t}}}{26}}+{ \frac { \left (506\,\cos \left (3\,t \right ) 798\,\sin \left (3\,t \right ) +299 \right ) {{\rm e}^{t}}}{234}} \]
Problem: Obtain unit step response of the second order system given by the transfer function \[ H\relax (s) =\frac {\omega _{n}^{2}}{s^{2}+2\xi \omega _{n}s+\omega _{n}^{2}}\] in order to illustrate the response when the system is over, under, and critically damped. use \(\omega _{n}=1\) and change \(\xi \) over a range of values that extends from under damped to over damped.
Mathematica
Matlab
Maple
Instead of using Simulate as above, another option is to use ResponsePlot which gives same plot as above.
Problem: Given the transfer function \[ H\relax (s) =\frac {\omega _{n}^{2}}{s^{2}+2\xi \omega _{n}s+\omega _{n}^{2}}\]
Display the output and input on the same plot showing how the steady state error changes as the un damped natural frequency \(\omega _{n}\) changes. Do this for ramp and step input.
The steady state error is the diﬀerence between the input and output for large time. In other words, it the diﬀerence between the input and output at the time when the response settles down and stops changing.
Displaying the curve of the output and input on the same plot allows one to visually see steady state error.
Use maximum time of \(10\) seconds and \(\xi =0.707\) and change \(\omega _{n}\) from \(0.2\) to \(1.2\).
Do this for ramp input and for unit step input. It can be seen that with ramp input, the steady state do not become zero even at steady state. While with step input, the steady state error can become zero.
These examples show how to use the inverse a Z transform.
Problem: Given \[ F\relax (z) =\frac {z}{z1}\] ﬁnd \(x[n]=F^{1}\relax (z)\) which is the inverse Ztransform.
Mathematica


Matlab


Problem: Given \[ F\relax (z) =\frac {5z}{\left (z1\right ) ^{2}}\] ﬁnd \(x[n]=F^{1}\relax (z) \)
In Mathematica analytical expression of the inverse Z transform can be generated as well as shown below
Mathematica Inverse Z is 5 n


Matlab


Find the Z transform for the unit step discrete function
Given the unit step function \(x[n]=u[n]\) deﬁned as \(x=\{1,1,1,\cdots \}\,\ \) for \(n\geq 0\,\), ﬁnd its Z transform.
Mathematica


Matlab


Find the Z transform for \(x[n]=\left (\frac {1}{3}\right ) ^{n}u\relax (n) +\left (0.9\right ) ^{n3}u\left ( n\right ) \)
Mathematica


Matlab


Given the following system, sample the input and ﬁnd and plot the plant output
Use sampling frequency \(f=1\) Hz and show the result for up to \(14\) seconds. Use as input the signal \(u(t)=\exp (0.3 t) \sin (2 \pi (f/3) t)\).
Plot the input and output on separate plots, and also plot them on the same plot.
Mathematica
Matlab
Problem: Given \[ L(s)=\frac {s}{(s+4)(s+5)}\] as the open loop transfer function, how to ﬁnd \(G(s)\), the closed loop transfer function for a unity feedback?
Mathematica




The system wrapper can be removed in order to obtain the rational polynomial expression as follows

\[ \frac {s}{s^2+10 s+20} \] 
Matlab


Mathematica

\[ \left ( {\begin {array}{cccccc} 3 & 1 & 1 & 1 & 0 & 0 \\ 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 0 & 2 & 0 & 1 & 1 \\ 0 & 0 & 0 & 2 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 & 1 \\ \end {array}} \right ) \] 

\[ \left ( {\begin {array}{cccccc} 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 2 & 1 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 & 0 & 0 \\ 0 & 0 & 0 & 2 & 1 & 0 \\ 0 & 0 & 0 & 0 & 2 & 1 \\ 0 & 0 & 0 & 0 & 0 & 2 \\ \end {array}} \right ) \] 
Matlab


Maple

\[ \left [\begin {array}{cccccc} 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 2 & 1 & 0 & 0 & 0 \\ 0 & 0 & 2 & 1 & 0 & 0 \\ 0 & 0 & 0 & 2 & 0 & 0 \\ 0 & 0 & 0 & 0 & 2 & 1 \\ 0 & 0 & 0 & 0 & 0 & 2 \end {array}\right ] \] 
Problem: Solve for \(X\) in the Riccati equation \[ A^{\prime }X+XAXBR^{1}B^{\prime }X+C^{\prime }C=0 \] given \begin {align*} A & =\begin {pmatrix} 3 & 2\\ 1 & 1 \end {pmatrix} \\ B & =\begin {pmatrix} 0\\ 1 \end {pmatrix} \\ C & =\begin {pmatrix} 1 & 1 \end {pmatrix} \\ R & =3 \end {align*}
Mathematica

\[ \left ( {\begin {array}{cc} 0.589517 & 1.82157 \\ 1.82157 & 8.81884 \\ \end {array}} \right ) \] 
Matlab


Maple

\[ \left [\begin {array}{cc} 0.5895174373 & 1.8215747249 \\ 1.8215747249 & 8.8188398069 \end {array}\right ] \] 
Problem: Given a continuoustime system represented by a transfer function \[ \frac {1}{s(s+0.5)}\] convert this representation to state space and sample the system at sampling period of \(1\) second, and then solve the discretetime Riccati equation.
The Riccati equation is given by \[ A^{\prime }X+XAXBR^{1}B^{\prime }X+C^{\prime }C=0 \]
Let \(R=[3]\).
Mathematica





\[ \left ( {\begin {array}{cc} 0.671414 & 0.977632 \\ 0.977632 & 2.88699 \\ \end {array}} \right ) \] 
Matlab






Maple

\[ \left [\begin {array}{cc} 0.6714144604 &  0.9776322436 \\  0.9776322436 & 2.8869912178 \end {array}\right ] \] 
Plot the impulse response of \(H(z)=z/(z^21.4 z+0.5)\) and using sampling period of \(T=0.5\) ﬁnd continuous time approximation using zero order hold and show the impulse response of the system and compare both responses.
Mathematica
Find its impulse response
approximate to continuous time, use ZeroOrderHold
Find the impulse response of the continuous time system
\[ 1.25559 e^{0.693147 t} (13.3012 \theta (t) \sin (0.283794 t)1. \theta (t) \cos (0.283794 t)) \]
Plot the impulse response of the discrete system
Plot the impulse response of the continuous system
Plot both responses on the same plot
Do the same plot above, using stair case approximation for the discrete plot
Matlab
Problem: Find the system type for the following transfer functions
To ﬁnd the system type, the transfer function is put in the form \(\frac {k\sum _{i}\left (ss_{i}\right ) }{s^{M}\sum _{j}\left (ss_{j}\right ) }\), then the system type is the exponent \(M\). Hence it can be seen that the ﬁrst system above has type one since the denominator can be written as \(s^{1}\left (s1\right )\) and the second system has type 2 since the denominator can be written as \(s^{2}\left (s1\right ) \) and the third system has type 5. The following computation determines the type
Mathematica

Out[171]= 1 
 Out[173]= 2 

Out[175]= 5 
Matlab






Problem, given the matrix \[ \left ( {\begin {array} [c]{ccc}1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {array}} \right ) \] Find its eigenvalues and eigenvectors.
Mathematica

\[ \left ( {\begin {array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end {array}} \right ) \] 
 \[ \left \{\frac {3}{2} \left (5+\sqrt {33}\right ),\frac {3}{2} \left (5\sqrt {33}\right ),0\right \} \] \[ \{16.1168,1.11684,0.\} \] 

\[ \left ( {\begin {array}{ccc} \frac {15\sqrt {33}}{33+7 \sqrt {33}} & \frac {4 \left (6+\sqrt {33}\right )}{33+7 \sqrt {33}} & 1 \\ \frac {15\sqrt {33}}{33+7 \sqrt {33}} & \frac {4 \left (6+\sqrt {33}\right )}{33+7 \sqrt {33}} & 1 \\ 1 & 2 & 1 \\ \end {array}} \right ) \] \[ \left ( {\begin {array}{ccc} 0.283349 & 0.641675 & 1. \\ 1.28335 & 0.141675 & 1. \\ 1. & 2. & 1. \\ \end {array}} \right ) \] 
Matlab Matlab generated eigenvectors are such that the sum of the squares of the eigenvector elements add to one.


Maple A:=Matrix([[1,2,3],[4,5,6],[7,8,9]]); evalf(LinearAlgebra:Eigenvectors(A))

First vector shows eigenvalues, and matrix on the right shows the eigenvectors in same order. \[ \left [ \left [\begin {array}{c} 16.1168439700 \\  1.1168439700 \\ 0.0 \end {array}\right ] , \left [\begin {array}{ccc} 0.2833494518 &  1.2833494520 & 1.0 \\ 0.6416747260 &  0.1416747258 &  2.0 \\ 1.0 & 1.0 & 1.0 \end {array}\right ] \right ] \] 
\[ \left ( {\begin {array} [c]{ccc}1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 0 \end {array}} \right ) \]
Mathematica

\[ x^3+6 x^2+72 x+27 \] 
Matlab Note: Matlab and Maple generated characteristic polynomial coeﬃcients are negative to what Mathematica generated. But the sign diﬀerence is not important.


Maple

\(x^{3}6 x^{2}72 x 27\) 
Problem, given the matrix \[ \left ( {\begin {array} [c]{cc}1 & 2\\ 3 & 2 \end {array}} \right ) \] Verify that matrix is a zero of its characteristic polynomial. The Characteristic polynomial of the matrix is found, then evaluated for the matrix. The result should be the zero matrix.
Mathematica

\(x^23 x4\) 

\[ \left ( {\begin {array}{cc} 0 & 0 \\ 0 & 0 \\ \end {array}} \right ) \] 
Another way is as follows

\[ \left ( {\begin {array}{cc} 0 & 0 \\ 0 & 0 \\ \end {array}} \right ) \] 
MATLAB has a buildin function polyvalm() to do this more easily than in Mathematica. Although the method shown in Mathematica can easily be made into a Matlab function
Matlab


Problem: Given a system Laplace transfer function, check if it is stable, then convert to state space and check stability again. In transfer function representation, the check is that all poles of the transfer function (or the zeros of the denominator) have negative real part. In state space, the check is that the matrix A is negative deﬁnite. This is done by checking that all the eigenvalues of the matrix A have negative real part. The poles of the transfer function are the same as the eigenvalues of the A matrix. Use \[ sys=\frac {5s}{s^{2}+4s+25}\]
Mathematica






Matlab




Mathematica








Matlab






Problem: Check the stability (in Lyapunov sense) for the state coeﬃcient matrix \[ A=\begin {bmatrix} 0 & 1 & 0\\ 0 & 0 & 1\\ 1 & 2 & 3 \end {bmatrix} \]
The Lyapunov equation is solved using lyap() function in MATLAB and LyapunovSolve[] function in Mathematica, and then the solution is checked to be positive deﬁnite (i.e. all its eigenvalues are positive).
We must transpose the matrix \(A\) when calling these functions, since the Lyapunov equation is deﬁned as \(A^TP+PA=Q\) and this is not how the software above deﬁnes them. By simply transposing the \(A\) matrix when calling them, then the result will be correct.
Mathematica

\[ \left ( {\begin {array}{ccc} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 2 & 3 \\ \end {array}} \right ) \] 
 \[ \left ( {\begin {array}{ccc} 2.3 & 2.1 & 0.5 \\ 2.1 & 4.6 & 1.3 \\ 0.5 & 1.3 & 0.6 \\ \end {array}} \right ) \] 

\(\{6.18272,1.1149,0.202375\}\) 
Matlab




Maple

\[ \left [ {\begin {array}{c} 6.18272045921436+ 0.0\,i \\ \noalign {\medskip } 1.11490451203192+ 0.0\,i\\ \noalign {\medskip } 0.202375028753723+ 0.0\,i\end {array}} \right ] \] 
Problem: Given the following block diagram, with sampling time \(T=0.1\ sec\), generate the closed loop transfer function, and that poles of the closed loop transfer function are inside the unit circle
System block diagram.
Mathematica
Now generate unit step response
\[ {\begin {array}{c} \{0.543991\, 0.325556 i,0.543991\, +0.325556 i\} \\ \end {array}} \]
\[ \left ( {\begin {array}{c} \{0.633966,0.633966\} \\ \end {array}} \right ) \]
Poles are inside the unit circle, hence stable.
Matlab
Problem: Given a system with 2 states, represented in state space, how to determine the state change due some existing initial conditions, when there is no input forces?
Mathematica




Matlab


Problem: Given a system represented by state space, how to determine the response \(y(t)\) due some existing initial conditions in the states. There is no input forces.
Mathematica




Matlab


Problem: Given a system represented by state space, how to determine the response with nonzero initial conditions in the states and when the input is a step input?
Mathematica




Matlab


Problem: Given \(L(s)\), the open loop transfer function, draw the root locus. Let \[ L(s)=\frac {s^2+2 s+4}{s(s+4)(s+6)(s^2+1.4 s+1)}\]
Root locus is the locus of the closed loop dominant pole as the gain \(k\) is varied from zero to inﬁnity.
Mathematica


Matlab


Mathematica
\[ \left ( {\begin {array}{cc} e^{2 t}+2 e^{t} & e^{2 t}+e^{t} \\ 2 e^{2 t}2 e^{t} & 2 e^{2 t}e^{t} \\ \end {array}} \right ) \]
Now verify the result by solving for \(e^{At}\) using the method would one would do by hand, if a computer was not around. There are a number of methods to do this by hand. The eigenvalue method, based on the Cayley Hamilton theorem will be used here. Find the eigenvalues of \(A\lambda I\)
\[ \left ( {\begin {array}{cc} \lambda & 1 \\ 2 & \lambda 3 \\ \end {array}} \right ) \]
\[ \lambda ^2+3 \lambda +2 \]
\[ \left \{\text {b0}\to e^{2 t} \left (2 e^t1\right ),\text {b1}\to e^{2 t} \left (e^t1\right )\right \} \]
\[ e^{2 t} \left (2 e^t1\right ) \]
\[ e^{2 t} \left (e^t1\right ) \]
\[ \left ( {\begin {array}{cc} e^{2 t} \left (1+2 e^t\right ) & e^{2 t} \left (1+e^t\right ) \\ 2 e^{2 t} \left (1+e^t\right ) & e^{2 t} \left (2+e^t\right ) \\ \end {array}} \right ) \] The answer is the same given by Mathematica’s command MatrixExp[]
Matlab
Maple
\[ \left [\begin {array}{cc} {\mathrm e}^{2 t}+2 \,{\mathrm e}^{t} & {\mathrm e}^{t}{\mathrm e}^{2 t} \\ 2 \,{\mathrm e}^{t}+2 \,{\mathrm e}^{2 t} & 2 \,{\mathrm e}^{2 t}{\mathrm e}^{t} \end {array}\right ] \]
Problem: Given the open loop for a continuous time system as \[ sys=\frac {5s+1}{s^{2}+2s+3}\]
convert to discrete time using a sampling rate \(0.5\) second, and display the root locus for the discrete system.
Mathematica




Matlab


Maple


Problem: Given the inverted pendulum shown below, use state space using one input (the force on the cart) and 2 outputs (the cart horizontal displacement, and the pendulum angle. Plot each output separately for the same input.
Mathematica
\[ \left ( {\begin {array}{cccc} 0 & 1 & 0 & 0 \\ 0 & 0 & \frac {g m}{M} & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & \frac {g (m+M)}{L M} & 0 \\ \end {array}} \right ) \]
\[ \left ( {\begin {array}{c} 0 \\ \frac {1}{M} \\ 0 \\ \frac {1}{L M} \\ \end {array}} \right ) \]
\[ \left ( {\begin {array}{cccc} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ \end {array}} \right ) \]
It is now possible to obtain the response of the system as analytical expression or an interpolatingFunction.
It is much more eﬃcient to obtain the response as interpolatingFunction. This requires that the time domain be given.
Here is example of obtaining the analytical expression of the response
\( {\begin {array}{l} e^{6.41561 t} (0.0238095 e^{6.41561 t} t^2 \theta (t)+0.000115693 e^{3.2078 t} \theta (t)0.000231385 e^{6.41561 t} \theta (t)+0.000115693 e^{9.62341 t} \theta (t))\\,e^{6.41561 t} (0.00242954 e^{3.2078 t} \theta (t)+0.00485909 e^{6.41561 t} \theta (t)0.00242954 e^{9.62341 t} \theta (t)) \end {array}} \)
Matlab
Given the following simple closed loop system, show the step response. Let mass \(M=1\text {kg}\), damping coeﬃcient \(c=1 \text {newtonseconds per meter}\) and let the stiﬀness coeﬃcient be \(k=20\text {newton per meter}\).
Using propertional controller \(J(s)=k_p\) where \(k_p=400\). First connect the system and then show \(y(t)\) for 5 seconds when the reference \(y_r(t)\) is a unit step function.
Mathematica
Matlab
Another way to do the above is
Problem: Given a standard second order system deﬁned by the transfer function \[ G\relax (s) =\frac {\omega _{n}^{2}}{s^{2}+2\zeta \omega _{n}s+\omega _{n}^{2}}\]
Change \(\zeta \) and plot the step response for each to see the eﬀect of changing \(\zeta \) (the damping coeﬃcient).
It is easy to solve this using the step command in Matlab, and similarly in Mathematica and Maple. But here it is solved directly from the diﬀerential equation.
The transfer function is written as \[ \frac {Y\relax (s) }{U\relax (s) }=\frac {\omega _{n}^{2}}{s^{2}+2\zeta \omega _{n}s+\omega _{n}^{2}}\] Where \(Y\relax (s) \) and \(U\relax (s) \) are the Laplace transforms of the output and input respectively.
Hence the diﬀerential equation, assuming zero initial conditions becomes\[ y^{\prime \prime }\relax (t) +2\zeta \omega _{n}\ y^{\prime }\left ( t\right ) +\omega _{n}^{2}\ y\relax (t) =\omega _{n}^{2}\ u\left ( t\right ) \] To solve the above in Matlab using ode45, the diﬀerential equation is converted to 2 ﬁrst order ODE’s as was done before resulting in\[\begin {bmatrix} x_{1}^{\prime }\\ x_{2}^{\prime }\end {bmatrix} =\begin {bmatrix} 0 & 1\\ \omega _{n}^{2} & 2\zeta \omega _{n}\end {bmatrix}\begin {bmatrix} x_{1}\\ x_{2}\end {bmatrix} +\begin {bmatrix} 0\\ \omega _{n}^{2}\end {bmatrix} u\relax (t) \]
For a step input, \(u\relax (t) =1\), we obtain
\[\begin {bmatrix} x_{1}^{\prime }\\ x_{2}^{\prime }\end {bmatrix} =\begin {bmatrix} 0 & 1\\ \omega _{n}^{2} & 2\zeta \omega _{n}\end {bmatrix}\begin {bmatrix} x_{1}\\ x_{2}\end {bmatrix} +\begin {bmatrix} 0\\ \omega _{n}^{2}\end {bmatrix} \]
Now ODE45 can be used. In Mathematica the diﬀerential equation is solved directly using DSolve and no conversion is needed.
Mathematica
Matlab
Problem: Plot the standard curves showing how the dynamic response \(R_{d}\) changes as \(r=\frac {\omega }{\omega _{n}}\) changes. Do this for diﬀerent damping ratio \(\xi \). Also plot the phase angle.
These plots are result of analysis of the response of a second order damped system to a harmonic loading. \(\omega \) is the forcing frequency and \(\omega _{n}\) is the natural frequency of the system.
Mathematica




Find and plot the step response of the plant \(\frac {1}{s^2+2s+1}\) connected to a PID controller with \(P=10, I=3.7, D=0.7\). Use negative closed loopback.
Mathematica


Matlab


Nyquist command takes as input the open loop transfer function (not the closed loop!) and generates a plot, which was can look at to determine if the closed loop is stable or not. The closed loop is assumed to be unity feedback. For example, if the open loop is \(G(s)\), then we know that the closed loop transfer function is \(\frac {G(s)}{1+G(s)}\). But we call Nyquist with \(G(s)\).
Here are two examples.
Given \(G(s)=\frac {s}{10.2s}\) generate Nyquist plot.
Mathematica


Matlab


Given \(G(s)=\frac {5(10.5s)}{s(1+0.1s)(10.25s)}\) generate Nyquist plot.
Mathematica


Matlab




However, there is a better function to do this called nyquist1.m which I downloaded and tried. Here is its results


Mathematica and Matlab allow one to do pretty much the same operations in the area of linear algebra and matrix manipulation. Two things to keep in mind is that Mathematica uses a more general way to store data.
Mathematica uses ragged arrays or a list of lists. This means rows can have diﬀerent sizes. (these are the lists inside the list). So a Mathematica matrix is stored in a list of lists. This is similar in a way to Matlab cell data structure, since each raw can have diﬀerent length. In a standard matrix each row must have the same length.
In Matlab one can also have ragged arrays, these are called cells. In Mathematica, there is one data structure for both.
Another thing to keep in mind is that Matlab, due to its Fortran background is column major when it comes to operations on matrices. This simple example illustrate this diﬀerence. Suppose we have a matrix \(A\) of 3 rows, and want to ﬁnd the location where \(A(i,j)=2\) where \(i\) is the row number and \(j\) is the column number. Given this matrix \[ A = \left ({\begin {array}{cccc} 1 & 2 & 3 & 4 \\ 2 & 3 & 1 & 5 \\ 5 & 6 & 7 & 2 \end {array}} \right )\] Then the result of using the find() command in Matlab is
The Matlab result gives the order of the rows it found the element at based on searching column wise since it lists the second row ﬁrst in its result. Compare this to Mathematica Position[] command
which gives
Mathematica searched rowwise.
I found Mathematica for matrix operations takes more time to get familiar with compared to Matlab’s, since Mathematica data structure is more general and therefore a little more complex (ragged arrays, or list of lists is its main data structure) compared to Matlab’s. This is because Mathematica has to also support symbolics in its commands and not just numerical data.
In Maple the following short cuts can be used enter vectors and matrices: For row vector: v:=<1234> and for column vector v:=<1,2,3,4> and for a Matrix of say 2 rows and 3 columns A:=<1,23,45,6>. Here  acts as a column separator. For transpose of matrix, A^%T is the short cut notation.
How to perform the following matrix/vector multiplication?
In Mathematica the dot ”.” is used for Matrix multiplication. In Matlab the ”*” is used. In Fortran, MATMUL is used.
Mathematica


Matlab


Julia
Same exact code as Matlab.
Ada
compile and run
Fortran
compile and run
Maple

\[ \left [ {\begin {array}{c} 14\\ 32\\ 50 \end {array}} \right ] \] 
Python


The problem is to insert a number into a vector given the index.
Mathematica


Matlab


Julia


Fortran


Maple Using <> notation


Python Python uses zero index.


The problem is to insert a row into the second row position in a 2D matrix
Mathematica


Matlab


Maple Using <<>> notation Using Matrix/Vector


Python


Fortran

Compile and run

The problem is to insert a column into the second column position in a 2D matrix.
Mathematica


Matlab


Fortran


Maple Using Matrix/Vector


Python


Given column vectors \(v1= \left [ {\begin {array}{c} 1\\ 2\\ 3 \end {array}} \right ]\) and \(v2= \left [ {\begin {array}{c} 4\\ 5\\ 6 \end {array}} \right ]\) and \(v3= \left [ {\begin {array}{c} 7\\ 8\\ 9 \end {array}} \right ]\) and \(v4=\left [ {\begin {array}{c} 10\\ 11\\ 12 \end {array}} \right ]\) generate the matrix \(m=\left [ {\begin {array}{cc} v_1&v_2\\ v_3&v_4 \end {array}} \right ] = \left [ {\begin {array}{cc} 1&4\\ \noalign {\medskip }2&5 \\ \noalign {\medskip }3&6\\ \noalign {\medskip }7&10\\ \noalign {\medskip } 8&11\\ \noalign {\medskip }9&12\end {array}} \right ] \)
Matlab was the easiest of all to do these operations with. No surprise, as Matlab was designed for Matrix and vector operations. But I was surprised that Maple actually had good support for these things, using its <> notation, which makes working with matrices and vectors much easier.
The command ArrayFlatten is essential for this in Mathematica.
Notice the need to use Transpose[{v}] in order to convert it to a column matrix. This is needed since in Mathematica, a list can be a row or column, depending on context.
Mathematica

\(\left ( {\begin {array}{cc} 1 & 4 \\ 2 & 5 \\ 3 & 6 \\ 7 & 10 \\ 8 & 11 \\ 9 & 12 \\ \end {array}} \right ) \) 
Matlab


Maple

\(\left [ {\begin {array}{cc} 1&4\\ \noalign {\medskip }2&5 \\ \noalign {\medskip }3&6\\ \noalign {\medskip }7&10\\ \noalign {\medskip } 8&11\\ \noalign {\medskip }9&12\end {array}} \right ] \) 
Python Another way


Fortran Using the RESHAPE command


Given mix of matrices and vectors, such as \(v1= \left [ {\begin {array}{c} 1\\ 2\\ 3 \end {array}} \right ]\) and \(v2= \left [ {\begin {array}{cc} 4& 5\\ 6& 7\\ 8& 9 \end {array}} \right ]\) and \(v3= \left [ {\begin {array}{c} 10\\ 11\\ 12 \end {array}} \right ]\) and \(v4=\left [ {\begin {array}{c} 13\\ 14\\ 15 \end {array}} \right ]\) and
\(v5=\left [ {\begin {array}{c} 16\\ 17\\ 18 \end {array}} \right ]\)
generate the matrix 6 by 3 matrix \(m= \left [ {\begin {array}{ccc} v1&v2\\v3&v4&v5\end {array}}\right ]= \left [ {\begin {array}{ccc} 1&4&5\\ \noalign {\medskip }2&6&7 \\ \noalign {\medskip }3&8&9\\ \noalign {\medskip }10&13&16 \\ \noalign {\medskip }11&14&17\\ \noalign {\medskip }12&15&18\end {array}} \right ] \)
Mathematica, thanks for function by Kuba at Mathematica stackexachage, this becomes easy to do
Mathematica
Maple
Matlab
Fortran
Mathematica




Matlab




Maple


Or
\[ \left [ {\begin {array}{cccc} 0.970592781760615697& 0.957506835434297598& 0.0975404049994095246& 0.126986816293506055\\ \noalign {\medskip } 0.157613081677548283& 0.546881519204983846& 0.632359246225409510& 0.905791937075619225\\ \noalign {\medskip } 0.964888535199276531& 0.278498218867048397& 0.913375856139019393& 0.814723686393178936\end {array}} \right ] \]
Fortran
Did not ﬁnd a buildin support for random numbers from normal distribution, need to look more.
Mathematica


Matlab


Maple

\[ \left [ {\begin {array}{cccc} 0&0&0&0\\ 0&0&0&0 \\ 0&0&0&0\end {array}} \right ] \] 
Fortran


Ada


Mathematica


Matlab


Maple




Fortran Using additional copy for the matrix


Problem: generate diagonal matrix with \(2,4,6,8\) on the diagonal.
Mathematica


Matlab


Maple


Mathematica


Matlab


Maple Another ways

15 
Mathematica

Out[49]= 45 
Matlab

ans = 45 
Maple

45 
A diagonal matrix is one which has only zero elements oﬀ the diagonal. The Mathematica code was contributed by Jon McLoone.
Mathematica


Maple

true 
The problem is to ﬁnd locations or positions of all elements in a matrix that are larger or equal than some numerical value such as \(2\) in this example.
Mathematica


Matlab


Maple Maple is weak in this area. There does not seem to be a buildin function to return indices in matrix based on some condition. I searched ListTools and ArrayTools. So had to program it in.


Given Matrix
Generate a new matrix of size \(2\) by \(3\) where each element of the new matrix is the above matrix. Hence the new matrix will look like
In Matlab, repmat() is used. In Mathematica, a Table command is used, followed by ArrayFlatten[]
Mathematica


Matlab


Another way is to use kron() in matlab, and KroneckerProduct in Mathematica and LinearAlgebra[KroneckerProduct] in Maple, which I think is a better way. As follows
Mathematica


Matlab


Maple


Mathematica


Matlab


Maple This below ﬁnds position of ﬁrst max.


Maple support for such operations seems to be not as strong as Matlab. One way to ﬁnd locations of all elements is by using explicit loop


Give a matrix
How to change it so that the second column becomes the ﬁrst, and the ﬁrst becomes the second? so that the result become
Mathematica


Matlab


Maple


Mathematica In Mathematica, to join 2 matrices sidebyside, use Join with '2' as the third argument. To join them one on top of the other, use '1' as the third argument








Matlab




Maple


Question posted on the net
Many answers were given, below is my answer, and I also show how to do it in Matlab
Mathematica


Matlab


Maple


Given a matrix A, and list of locations within the matrix, where each location is given by \(i,j\) entry, ﬁnd the value in the matrix at these locations
Example, given
obtain the entries at \(1,1\) and \(3,3\) which will be \(1\) and \(9\) in this example.
Mathematica


Another method (same really as above, but using Part explicit)


Matlab


Maple


Given
covert the matrix to one vector
Mathematica


Matlab


Maple Maple reshapes along columns, like Matlab. To get same result as Mathematica, we can transpose the matrix ﬁrst. To get same result as Matlab, do not transpose.


Example, given Matrix
Select rows which has \(9\) in the second column and \(10\) in the last column. Hence the result will be the ﬁrst and third rows only
Mathematica




Matlab


Maple

\[ \left [\begin {array}{cccc} 1 & 9 & 0 & 10 \\ 3 & 9 & 2 & 10 \end {array}\right ] \] 
There does not seem to be a direct way in Maple to take list of row Vectors, and use these to make a Matrix. This is why I had to convert to list in the above to make it work.
Given
Select elements in the ﬁrst column only which has corresponding element in the second column greater than 3, hence the result will be
Mathematica


another way is to ﬁnd the index using Position and then use Extract


another way is to use Cases[]. This is the shortest way


Matlab


Maple

\([6, 7, 2]\) 
The problem is to select rows in a matrix based on string value in the ﬁrst column. Then sum the total in the corresponding entries in the second column. Given. For example, given
and given list
The problem is to select rows in which the string in the list is part of the string in the ﬁrst column in the matrix, and then sum the total in the second column for each row found. Hence the result of the above should be
Mathematica


Matlab


But notice that in Matlab, the answer is a cellarray. To access the numbers above


Given: square matrix, and list which represents the index of rows to be removed, and it also represents at the same time the index of the columns to be removed (it is square matrix, so only one list is needed).
output: the square matrix, with BOTH the rows and the columns in the list removed.
Assume valid list of indices.
This is an example: remove the second and fourth rows and the second and fourth columns from a square matrix.
I asked this question at SO, and more methods are shown there at HTML
Mathematica Three methods are shown. method 1: (credit for the idea to Mike Honeychurch at stackoverﬂow). It turns out it is easier to work with what we want to keep instead of what we want to delete so that Part[] can be used directly. Hence, given a list of row numbers to remove, such as Start by generating list of the rows and columns to keep by using the command Complement[], followed by using Part[]


method 2: (due to Mr Wizard at stackoverﬂow)


method 3: (me) use Pick. This works similar to Fortran pack(). Using a mask matrix, we set the entry in the mask to False for those elements we want removed. Hence this method is just a matter of making a mask matrix and then using it in the Pick[] command.


Matlab


Maple


Problem: given a list of numbers such as
convert the above to list of strings
Mathematica


Matlab


answer below is due to Bruno Luong at Matlab newsgroup


Maple


Given vector or list \(d=[9,1,3,3,50,7,19],\) \(t=[0,7,2,50]\), ﬁnd the common elements.
Mathematica


Matlab


Maple


Given
Sort each column on its own, so that the result is
In Matlab, the sort command is used. But in the Mathematica, the Sort command is the same the Matlab’s sortrows() command, hence it can’t be used as is. Map is used with Sort to accomplish this.
Mathematica


Matlab


Maple


Given
Sort each row on its own, so that the result is
Mathematica


Matlab


Maple


I had to convert each row to a list above for this to work. But for the case of columns (see last item earlier), this was not needed. Strange that one can’t not construct a Matrix from list of Row vectors as with a list of Column vectors.
Maple 2021.
Given
Sort the matrix rowwise using ﬁrst column as key so that the result is
In Matlab, the sortrows() command is used. In Mathematica the Sort[] command is used as is. No need to do anything special.
Mathematica


Matlab


Maple


Given
Sort the matrix rowwise using the second column as key so that the result is
In Matlab, the sortrows() command is used, but now we tell it to use the second column as key.
In Mathematica the SortBy[] command is now used but we tell it to use the second slot as key.
Mathematica


Matlab


Maple


Problem: Given a matrix, replace the ﬁrst nonzero element in each row by some a speciﬁc value. This is an example. Given matrix \(A\) below, replace the ﬁrst nonzero element in each row by \(1\), then
\(A=\begin {pmatrix} 50 & 75 & 0\\ 50 & 0 & 100\\ 0 & 75 & 100\\ 75 & 100 & 0\\ 0 & 75 & 100\\ 0 & 75 & 100 \end {pmatrix} \) will become \(B=\begin {pmatrix} 1 & 75 & 0\\ 1 & 0 & 100\\ 0 & 1 & 100\\ 1 & 100 & 0\\ 0 & 1 & 100\\ 0 & 1 & 100 \end {pmatrix} \)
Mathematica Solution due to Bob Hanlon (from Mathematica newsgroup) Solution by Fred Simons (from Mathematica newsgroup) Solution due to Adriano Pascoletti (from Mathematica newsgroup) Solution due to Oliver Ruebenkoenig (from Mathematica newsgroup) Solution due to Szabolcs Horvát (from Mathematica newsgroup)


Matlab This solution due to Bruno Luong (from matlab newsgroup) This solution due to Jos from matlab newsgroup


Maple


Problem: Given 2 vectors, perform outer product and outer sum between them. The outer operation takes the ﬁrst element in one vector and performs this operation on each element in the second vector. This results in ﬁrst row. This is repeated for each of the elements in the ﬁrst vector. The operation to perform can be any valid operation on these elements.
Mathematica using symbolic vectors. Outer product


Outer sum


using numerical vectors. Outer product


Outer sum


Matlab Outer product


Outer sum


Maple Due to Carl Love from the Maple newsgroup




Problem: Find the rank and nullities of the following matrices, and ﬁnd the bases of the range space and the Null space.
\( A=\begin {pmatrix} 2 & 3 & 3 & 4 \\ 0 & 1 & 2 & 2 \\ 0 & 0 & 0 & 1 \end {pmatrix} \)
Mathematica

3,4 






Matlab


Maple


Problem: Find the SVD for the matrix \(\begin {bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end {bmatrix} \) Notice that in Maple, the singular values matrix, normally called S, is returned as a column vector. So need to call DiagonalMatrix() to format it as expected.
Mathematica




Matlab




Maple










Solve for x in the following system of equations
Mathematica


Matlab


Maple


Fortran
compile and run
Given a matrix, ﬁnd the locations and the values of all nonzero elements. Hence given the matrix
\[ \left ({\begin {array}{ccc} 0 & 0 & 1 \\ 10 & 0 & 2 \\ 3 & 0 & 0 \end {array}} \right )\]
the positions returned will be \((1,3),(2,1),(2,3),(3,1)\) and the corresponding values are \(1,10,2,3\).
Mathematica In Mathematica, standard Mathematica matrix operations can be used, or the matrix can be converted to SparseArray and special named operation can be used on it.




Or standard list operations can be used




Matlab




Maple


Given a function \(f(x)\) evaluate it for each value contained in a vector. For example, given \(f(x)=x^2\) evaluate it on \((1,2,3)\) such that the result is \((1,4,9)\).
Mathematica


Matlab


Maple


Mathematica

Matlab

Maple


Evaluate \(x \exp ^{x^2y^2}\) on 2D cartesian grid between \(x=2 \cdots 2\) and \(y=4 \cdots 4\) using \(h=0.2\) for grid spacing.
Mathematica
The above can also be done using Plot3D
I need to sort out the orientation diﬀerence between the two plots above.
Matlab
Maple
Given a square matrix, ﬁnd its determinant. In Mathematica, the Det[] command is used. In Matlab the det() command is used.
Mathematica


Matlab


Maple


Given matrix \(\begin {pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {pmatrix} \), generate the following sparse matrix with this matrix on the diagonal
\[\begin {pmatrix} 1 & 2 & 3 & 0 & 0 & 0 & 0 & 0 & 0\\ 4 & 5 & 6 & 0 & 0 & 0 & 0 & 0 & 0\\ 7 & 8 & 9 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 2 & 3 & 0 & 0 & 0\\ 0 & 0 & 0 & 4 & 5 & 6 & 0 & 0 & 0\\ 0 & 0 & 0 & 7 & 8 & 9 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 3\\ 0 & 0 & 0 & 0 & 0 & 0 & 4 & 5 & 6\\ 0 & 0 & 0 & 0 & 0 & 0 & 7 & 8 & 9 \end {pmatrix} \]
Mathematica

\[ \left ( {\begin {array}{ccccccccc} 1. & 2. & 3. & 0 & 0 & 0 & 0 & 0 & 0 \\ 4. & 5. & 6. & 0 & 0 & 0 & 0 & 0 & 0 \\ 7. & 8. & 9. & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1. & 2. & 3. & 0 & 0 & 0 \\ 0 & 0 & 0 & 4. & 5. & 6. & 0 & 0 & 0 \\ 0 & 0 & 0 & 7. & 8. & 9. & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1. & 2. & 3. \\ 0 & 0 & 0 & 0 & 0 & 0 & 4. & 5. & 6. \\ 0 & 0 & 0 & 0 & 0 & 0 & 7. & 8. & 9. \\ \end {array}} \right ) \] 
Matlab




Maple


The second derivative \(\frac {d^{2}u}{dx^{2}}\) is approximated by \(\frac {u_{i1}2u_{i}+u_{i+1}}{h^{2}}\) where \(h\) is the grid spacing. Generate the \(A\) matrix that represent this operator for \(n=4\) where \(n\) is the number of internal grid points on the line
Mathematica

\[ \left ( {\begin {array}{cccccccccccc} 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 \\ \end {array}} \right ) \] 
Matlab


Maple

\[ \left [\begin {array}{cccccccccccc} 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 \end {array}\right ] \] 
\(\nabla ^{2}u=f\) in 2D is deﬁned as \(\frac {\partial ^{2}u}{\partial x^{2}}+\frac {\partial ^{2}u}{\partial y^{2}}=f\) and the Laplacian operator using second order standard diﬀerences results in \(\left (\frac {u_{i1,j}+u_{i+1,j}+u_{i,j1}+u_{i,j+1}4u_{i,j}}{h^{2}}\right ) =f_{i,j}\) where \(h\) is the grid size. The above is solved for \(x\) in the form \(Ax=f\) by generating the \(A\) matrix and taking into consideration the boundary conditions. The follows show how to generate the sparse representation of \(A\). Assuming the number of unknowns \(n=3\) in one direction, there are 9 unknowns to solve for and the \(A\) matrix will be \(9\) by \(9\).
Matlab


The goal is to solve\[ \frac {\partial ^{2}u}{\partial x^{2}}+\frac {\partial ^{2}u}{\partial y^{2}}+\frac {\partial ^{2}u}{\partial z^{2}}=f(x,y,z) \] On the unit cube. The following diagram was made to help setting up the 3D scheme to approximate the above PDE
The discrete approximation to the Laplacian in 3D is\[ \frac {\partial ^{2}u}{\partial x^{2}}+\frac {\partial ^{2}u}{\partial y^{2}}+\frac {\partial ^{2}u}{\partial z^{2}}=\frac {1}{h^{2}}\left (U_{i1,j,k}+U_{i+1,j,k}+U_{i,j1,k}+U_{i,j+1,k}+U_{i,k,k1}+U_{i,j,k+1}6U_{i,j,k}\right ) \] For the direct solver, the \(A\) matrix needs to be formulated. From\[ \frac {1}{h^{2}}\left (U_{i1,j,k}+U_{i+1,j,k}+U_{i,j1,k}+U_{i,j+1,k}+U_{i,k,k1}+U_{i,j,k+1}6 U_{i,j,k}\right ) =f_{i,j,k}\] Solving for \(U_{i,j,k}\) results in\[ U_{i,j,k}=\frac {1}{6}\left (U_{i1,j,k}+U_{i+1,j,k}+U_{i,j1,k}+U_{i,j+1,k}+U_{i,k,k1}+U_{i,j,k+1}h^{2}f_{i,j,k}\right ) \] To help make the \(A\) matrix, a small example with \(n=2,\) is made. The following diagram uses the standard numbering on each node
By traversing the grid, left to right, then inwards into the paper, then upwards, the following \(A\) matrix results
The recursive pattern involved in these \(A\) matrices can now be seen. Each \(A\) matrix contains inside it a block on its diagonal which repeats \(n\) times. Each block in turn contain inside it, on its diagonal, smaller block, which also repeats \(n\) times.
It is easier to see the pattern of building \(A\) by using numbers for the grid points, and label them in the same order as they would be visited, this allowes seeing the connection between each grid point to the other easier. For example, for \(n=2\),
The connections are now more easily seen. Grid point 1 has connection to only points \(2,3,5\). This means when looking at the \(A\) matrix, there will be a \(1\) in the ﬁrst row, at columns \(2,3,5\). Similarly, point \(2\) has connections only to \(1,4,6\), which means in the second row there will be a \(1\) at columns \(1,4,6\). Extending the number of points to \(n=3\) to better see the pattern of \(A\) results in
From the above it is seen that for example point \(1\) is connected only to \(2,4,10\) and point \(2\) is connected to \(1,3,5,11\) and so on.
The above shows that each point will have a connection to a point which is numbered \(n^{2}\) higher than the grid point itself. \(n^{2}\) is the size of the grid at each surface. Hence, the general \(A\) matrix, for the above example, can now be written as
The recursive structure can be seen. There are \(n=3\) main repeating blocks on the diagonal, and each one of them in turn has \(n=3\) repeating blocks on its own diagonal. Here \(n=3\), the number of grid points along one dimension.
Now that the \(A\) structure is understood, the A matrix can be generated.
Example 1: Using \(n_x=2\), \(n_y=2\), \(n_z=2\). These are the number of grid points in the \(x,y,z\) directions respectively.
Matlab


Example 2: Using \(n_x=2\), \(n_y=2\), \(n_z=3\).
Matlab


Example 3: Using \(n_x=3\), \(n_y=3\), \(n_z=3\).
Matlab
Matlab result is below.
Given square matrix such as
\[\begin {pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end {pmatrix} \]
ﬁnd the adjugate matrix which is
\[\begin {pmatrix} 3 & 6 & 3 \\ 6 & 12 & 6 \\ 3 & 6 & 3 \\ \end {pmatrix} \]
Mathematica


Maple

\[ \left [\begin {array}{ccc} 3 & 6 & 3 \\ 6 & 12 & 6 \\ 3 & 6 & 3 \end {array}\right ] \] 
Matlab Will try to ﬁnd function in Matlab to do this. But for nonsingular matrices only the direct method of ﬁnding the inverse and then multiplying by the determinant to recover the adjunct can be used.


The following is due to Matt J from the matlab newsgroup


Fortran
Thanks goes to James Van Buskirk and Louisa from comp.lang.fortran for the review and suggestions which lead to improvements of the code.
Ada
compile and run
Given a row of the same number of values as the number of columns in a matrix, how to scale each column by the corresponding value in that row? This picture explains more the problem
In Matlab, bsxfun is used.
credit for this solution goes to Bob Hanlon, Adriano Pascoletti, Kurt Tekolste, David Park, and Peter. J. C. Moses from the Math group
{{2, 6}, {6, 12}, {10, 18}}
Another way is to use Inner[] command. Credit for this solution goes to Sswziwa Mukasa and Peter. J. C. Moses from the Math group
Out[66]= {{2, 6}, {6, 12}, {10, 18}}
ans = 2 6 6 12 10 18
#compile and run >gfortran std=f2008 t45.f90 >./a.out 2 6 6 12 10 18
\[ \left [ \begin {array}{cc} 2&6\\ \noalign {\medskip }6&12 \\ \noalign {\medskip }10&18\end {array} \right ] \]
Given 2D matrix \(A\) extract all submatrices by removing row/column.
For example, given the matrix \(\begin {pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {pmatrix} \) then the submatrix for \(\left (1,1\right ) \) is\(\begin {pmatrix} 5 & 6\\ 8 & 9 \end {pmatrix} \) obtained by removing the ﬁrst row and the ﬁrst column.
In Mathematica, ReplacePart can be used. In Matlab the [] operator can be used. In Fortran, the pack() function can be used.

{{{5,6},{8,9}}, {{4,6},{7,9}}, {{4,5},{7,8}}, {{2,3},{8,9}}, {{1,3},{7,9}}, {{1,2},{7,8}}, {{2,3},{5,6}}, {{1,3},{4,6}}, {{1,2},{4,5}}}


B = 5 6 8 9 B = 4 6 7 9 B = 4 5 7 8 B = 2 3 8 9 B = 1 3 7 9 B = 1 2 7 8 B = 2 3 5 6 B = 1 3 4 6 B = 1 2 4 5

compile and run
>gfortran std=f2008 fcheck=all Wall Wextra Wconversionextra t46.f90 >./a.out 5.0 6.0 8.0 9.0 4.0 6.0 7.0 9.0 4.0 5.0 7.0 8.0 2.0 3.0 8.0 9.0 1.0 3.0 7.0 9.0 1.0 2.0 7.0 8.0 2.0 3.0 5.0 6.0 1.0 3.0 4.0 6.0 1.0 2.0 4.0 5.0
compile and run
>gnatmake foo.adb gcc4.6 c foo.adb gnatbind x foo.ali gnatlink foo.ali >./foo >./foo 5.00000E+00 6.00000E+00 8.00000E+00 9.00000E+00 4.00000E+00 6.00000E+00 7.00000E+00 9.00000E+00 4.00000E+00 5.00000E+00 7.00000E+00 8.00000E+00 2.00000E+00 3.00000E+00 8.00000E+00 9.00000E+00 1.00000E+00 3.00000E+00 7.00000E+00 9.00000E+00 1.00000E+00 2.00000E+00 7.00000E+00 8.00000E+00 2.00000E+00 3.00000E+00 5.00000E+00 6.00000E+00 1.00000E+00 3.00000E+00 4.00000E+00 6.00000E+00 1.00000E+00 2.00000E+00 4.00000E+00 5.00000E+00
\[ \left [\left [\begin {array}{cc} 5 & 6 \\ 8 & 9 \end {array}\right ], \left [\begin {array}{cc} 4 & 6 \\ 7 & 9 \end {array}\right ], \left [\begin {array}{cc} 4 & 5 \\ 7 & 8 \end {array}\right ], \left [\begin {array}{cc} 2 & 3 \\ 8 & 9 \end {array}\right ], \left [\begin {array}{cc} 1 & 3 \\ 7 & 9 \end {array}\right ], \left [\begin {array}{cc} 1 & 2 \\ 7 & 8 \end {array}\right ], \left [\begin {array}{cc} 2 & 3 \\ 5 & 6 \end {array}\right ], \left [\begin {array}{cc} 1 & 3 \\ 4 & 6 \end {array}\right ], \left [\begin {array}{cc} 1 & 2 \\ 4 & 5 \end {array}\right ]\right ] \]
Example, Given the folowing 2D matrix \(A\) delete the second row
\(\begin {pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {pmatrix} \)
Example, Given the folowing 2D matrix \(A\) delete say the second column
\(\begin {pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {pmatrix}\)

Out[93]= {{1,3}, {4,6}, {7,9}}

or a little longer solution using Pick

Out[98]= {{1,3}, {4,6}, {7,9}}


\[ \left [ \begin {array}{cc} 1&3\\ \noalign {\medskip }4&6 \\ \noalign {\medskip }7&9\end {array} \right ] \] 
Generate the random matrix and divide each row by its total
Generate the random matrix and divide each column by its total
[ 9 95 51 24] [ ] A := [99 20 76 65] [ ] [60 25 44 86]
b := [168, 140, 83, 175]
[3 19 51 24 ] [   ] [56 28 83 175] [ ] [33 1 76 13] B := [   ] [56 7 83 35] [ ] [5 5 44 86 ] [   ] [14 28 83 175]
[1, 1, 1, 1]
Given the folowing 2D matrix \(A\) ﬁnd the sum of each row
\[ \begin {pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {pmatrix} \]
Given the folowing 2D matrix \(A\) ﬁnd the sum of each column
\(\begin {pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end {pmatrix}\)
Mathematica

Matlab

Maple


given matrix \(\begin {pmatrix} 0 & 39 & 0\\ 55 & 100 & 0\\ 34 & 0 & 0\\ 0 & 9 & 0\\ 0 & 0 & 50 \end {pmatrix} \) ﬁnd the column index which contain values \(>0\) but do it from top row to the bottom row. Hence the result should be \(\left ( \overset {1st\ \operatorname {row}}{\overbrace {2}},\overset {2nd\ \operatorname {row}}{\overbrace {1,2}},\overbrace {1},\overbrace {2},\overbrace {3}\right ) \) this is becuase on the ﬁrst row, nonzero is in second column, and on the second row, nonzero is at ﬁrst and second column, and on the third row, nonzero is at the ﬁrst column, and so on.
Given vector \(v1=\{1,3,4,5,8,9,20.2,30,44\}\) remove from it any element that exist in \(v2=\{1,7,4\}\). Notice that Complement[] in Mathematica or setdiff() in Matlab can be used, but they will sort the result. Hence they are not used here in order to keep the order the same.

{3,5,8,9,20.2,30,44} 

{3,5,8,9,20.2,30,44} 
>gfortran f2.f90 >./a.out 3.0000000 5.0000000 8.0000000 9.0000000 20.200001 30.000000 44.000000
Given vector \(V\) of length \(m\) ﬁnd the mean of segments \(V(1:n),V(n+1:2n),V(2n+1:3n)\dots \). In otherwords, equal length segments.
Mathematica


Matlab


Given matrix \[ \left ({\begin {array}{ccc} 1 & 5 \\ 2 & 3 \\ 4 & 8 \\ 7 & 2 \end {array}} \right ) \]
search in column 2 of matrix for the ﬁrst time a value exceeds 6 and return the matrix up to that row. The result should be
\[ \left ({\begin {array}{ccc} 1 & 5 \\ 2 & 3 \end {array}} \right ) \]
Mathematica


Matlab


Maple

\[ \left [\begin {array}{cc} 1 & 5 \\ 2 & 3 \end {array}\right ] \] 
Do the following transformation. Take each element of matrix, and replace it by a 2 by 2 matrix with its values in places.
kron() in Matlab and KroneckerProduct in Mathematica and Maple can be used for this.
Mathematica Another method that can be used in this, but I prefer the kron method above:


Matlab


Maple

\[ \left [\begin {array}{cccccc} 1 & 1 & 2 & 2 & 3 & 3 \\ 1 & 1 & 2 & 2 & 3 & 3 \\ 4 & 4 & 5 & 5 & 6 & 6 \\ 4 & 4 & 5 & 5 & 6 & 6 \\ 7 & 7 & 8 & 8 & 9 & 9 \\ 7 & 7 & 8 & 8 & 9 & 9 \end {array}\right ] \] 
Gives a matrix, repeate each column a number of time, say 3 times, in place to produce a matrix 3 times as wide as the original.
kron() in Matlab and KroneckerProduct in Mathematica and Maple can be used for this.
Mathematica Another method that can be used in this, but the above is better


Matlab


Maple

\[ \left [\begin {array}{cccccccccccc} 1 & 1 & 1 & 2 & 2 & 2 & 3 & 3 & 3 & 4 & 4 & 4 \\ 5 & 5 & 5 & 6 & 6 & 6 & 7 & 7 & 7 & 8 & 8 & 8 \\ 9 & 9 & 9 & 10 & 10 & 10 & 11 & 11 & 11 & 12 & 12 & 12 \end {array}\right ] \] 
Apply function \(f(x,n)\) to ragged matrix where \(n\) is value taken from the matrix and \(x\) is some other value deﬁned somewhere else. The result of the function should replace the same position in the matrix where \(n\) was.
For example, given matrix \[ mat = \left ({\begin {array}[c]{ccc}1 & 2 & 3\\ 4 & 5 & \\ 7 & 8 & 9 \end {array}}\right ) \]
generate the matrix
\[ \left ({\begin {array} [c]{ccc}f[x,1] & f[x,2] & f[x,3]\\ f[x,4] & f[x,5] & \\ f[x,7] & f[x,8] & f[x,9] \end {array}}\right ) \]
Mathematica The trick is to use Map with 2 at end.

\( \{\{f(x,1),f(x,2),f(x,3)\},\{f(x,4),f(x,5)\},\{f(x,7),f(x,8),f(x,9)\}\} \) 
Maple


Given a vector or list \(1,2,3,4,5,6,7,8,9,10\) how to ﬁnd the sum of all its elements?
Mathematica



55 
Matlab

55 
Maple

55 
Given \[ \left ({\begin {array} {cc} 1 & 2 \\ 3 & 4 \end {array}} \right ) \] Find the maximum of each row. The result should be \((2,4)\). (to ﬁnd the min, just change Max with Min below.
Mathematica


Matlab


Maple


Given \[ \left ({\begin {array} {cc} 1 & 2 \\ 3 & 4 \end {array}} \right ) \] Find the maximum of each column. The result should be \((3,4)\). (to ﬁnd the min, just change Max with Min below.
Mathematica


Matlab


Maple


Given \[ \left ({\begin {array} {cc} 1 & 2 \\ 3 & 4 \end {array}} \right ) \] Find the mean of each column, and add this mean to each element of the corresponding column. The result should be
\[ \left ({\begin {array} {cc} 3 & 5 \\ 5 & 7 \end {array}} \right ) \]
To subtract the mean, just replace Plus with Subtract below for Mathematica and replace @plus with @minus for Matlab. This shows that Matlab bsxfun is analogue to Mathematica’s MapThread.
Mathematica


Matlab


Maple Maple mean always return real value, even if exact is possible.

\[ \left [\begin {array}{cc} 3.0 & 5.0 \\ 5.0 & 7.0 \end {array}\right ] \] 
Given \[ \left ({\begin {array} {cc} 1 & 2 \\ 3 & 4 \end {array}} \right ) \] Find the mean of each row, and add this mean to each element of the corresponding row. The result should be
\[ \left ({\begin {array} {cc} 2.5 & 3.5 \\ 6.5 & 7.5 \end {array}} \right ) \]
To subtract the mean, just replace Plus with Subtract below for Mathematica and replace @plus with @minus for Matlab. This shows that Matlab bsxfun is analogue to Mathematica’s MapThread.. The main diﬀerence is that Matlab is a little bit more consistent in this example, since in Matlab all operations are done column wise. Hence mean(A) takes the mean of each column (same as Matematica in this one case), but bsxfun also acts column wise on the matrix A, while Mathematica Map and MapThread act row wise (list of lists). One just needs to be careful about this order diﬀerence.
Mathematica


Matlab


Maple

\[ \left [\begin {array}{cc} 2.5000000000 & 3.5000000000 \\ 6.5000000000 & 7.5000000000 \end {array}\right ] \] 
Problem: Given the vector say \[ v={1,2,4} \]
Find its norm for \(p=1,2,\infty \)
Mathematica


Matlab


Problem: Given a matrix A, check to see if it is Hermite.
A Matrix is Hermite if it is the same as the conjugate of its transpose. One way to check is to take the diﬀerence and check that all values in the resulting diﬀerence matrix are zero.
To account for numerical values, the check is done using machine epsilon.
Mathematica

True 
Matlab





1 
Maple

true 
Problem: Given a matrix \(A\), ﬁnd matrix \(L\) and \(U\) such that \(LU=A\). The matrix \(L\) will have \(1\) in all the diagonal elements and zeros in the upper triangle. The matrix \(U\) will have \(0\) in the lower triangle as shown in this diagram.
Mathematica








Matlab




Maple

\[ \left [ \left [\begin {array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end {array}\right ] , \left [\begin {array}{ccc} 1 & 0 & 0 \\ 2 & 1 & 0 \\ 1 & \frac {5}{2} & 1 \end {array}\right ] , \left [\begin {array}{ccc} 1 & 3 & 5 \\ 0 & 2 & 3 \\ 0 & 0 & \frac {3}{2} \end {array}\right ] \right ] \] 
Problem: Given 2 sequences \(x_{1}[n]\) and \(x_{2}[m]\), determine the linear convolution of the 2 sequences. Assume \(x_{1}=[1,2,3,4,5]\) and \(x_{2}=[3,4,5]\).
Mathematica


Matlab


Maple In Maple, had to convert lists to Array ﬁrst to use Convolution. This is not good. The function should have accepted lists also. Maple 2021.

\[ \left [\begin {array}{ccccccc} 4.0 & 13.0 & 28.0 & 43.0 & 58.0 & 49.0 & 30.0 \end {array}\right ] \] 
Problem: Given 2 sequences \(x_{1}[n]\) and \(x_{2}[m]\), determine the circular convolution of the 2 sequences.
Mathematica


Matlab


Maple Note: had to ﬁgure the correct normalization to get same answer as Matlab. Also, the input has to be type Array. list is not accepted.

\[ \left [\begin {array}{cccc} 14.0 & 16.0 & 14.0 & 16.0 \end {array}\right ] \] 
Mathematica


Matlab


Maple Padding zeros to make the two list same was a little tricky. Find if there is a better way.

\[ \left [\begin {array}{ccccc} 53.0 & 43.0 & 28.0 & 43.0 & 58.0 \end {array}\right ] \] 
For simple case where the 2 sequences are assumed to start at n=0, then this can be done in the time domain using the ListConvolve in Mathematica and using conv in Matlab.
The harder case is when the origin is not located at the ﬁrst element in the sequence. I.e. one or both of the sequences is not causal.
Mathematica
case 1
Convolve 2 sequences where the origin is assumed to be at the ﬁrst element in each sequence.
case 2
Convolve 2 sequences where the origin is located at diﬀerent location from the ﬁrst element of the sequence, use DiracDelta function, and the DTFT approach.
Example convolve x=1,2,0,2,1,4,01,2,2 with h=1/4,1/2,1/4 where the origin of h is under the second element 1/2.
\[ \frac {1}{4} e^{i w} \left (1+e^{i w}\right )^2 \]
Write down the x sequence and take its DTFT
\[ e^{7 i w} \left (e^{i w}e^{2 i w}+4 e^{3 i w}+e^{4 i w}2 e^{5 i w}+2 e^{6 i w}+e^{7 i w}+2\right ) \]
Now multiply the DTFT’s and take the inverse
Now convolve z with h again, where z is the convolution of x and h found above. This can be done as follows in one command
Problem: Given a 2 dimensional matrix, say \(m\times n\,\), how to visualize its content?
These are some examples showing how to visualize a matrix as a 3D data, where the height is taken as the values of the matrix entries, and the \(x,y\,\ \)indices as the coordinates.
Mathematica
Matlab
Maple
Problem: Given \begin {align*} A & =[0,0,2,1,3,7,1,2,3,0,0]\\ B & =[0,0,1,1,2,2,4,1,2,5,0,0] \end {align*}
Notice that the output sequence generated by Mathematica and Matlab are reversed with respect to each others.
Also, MATLAB uses the length \(2N1\) as the length of cross correlation sequence, which in this example is 23 because \(N\) is taken as the length of the larger of the 2 sequences if they are not of equal length which is the case in this example.
In Mathematica, the length of the cross correlation sequence was 22, which is \(2N\).
Maple
\[ [ 7.0, 0.0, 33.0, 14.0, 36.0, 19.0, 9.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] \]
Not able to ﬁnd out now why Maple result is diﬀerent. May be deﬁnition used is diﬀerent, no time now to ﬁnd out.
Problem: Given the matrix \(A\) whose columns represents some vectors, ﬁnd the set of orthonormal vectors that span the same space as \(A\) and verify the result. Let \[ A=\begin {bmatrix} 0 & 1 & 1 & 2\\ 1 & 2 & 3 & 4\\ 2 & 0 & 2 & 0 \end {bmatrix} \]
Notice that \(A\) has rank 2, so we should get no more than 2 vectors in the orthonormal set.
With MATLAB use the orth(A) function, With Mathematica, use {u,s,v}=SingularValueDecomposition[A] , and since the rank is 2, then the ﬁrst 2 columns of matrix u will give the answer needed (any 2 columns of u will also give a set of orthonormal vectors).
Mathematica

2 




Matlab




Problem: Solve for x given that \(A x=b\) where \[ A=\left ( {\begin {array} [c]{cc}1 & 2\\ 1 & 3 \end {array}} \right ) \] and \[ b=\left ( {\begin {array} [c]{c}5\\ 8 \end {array}} \right ) \]
These 2 equations represent 2 straight lines in a 2D space. An exact solution exist if the 2 lines intersect at a point. The 2 lines are \(x+2y=5\) and \(x+3y=8\).
Mathematica




Matlab


Problem: Given a general non homogeneous set of linear equations \(Ax=b\) how to test if it has no solution (inconsistent), or one unique solution, or an inﬁnity number of solutions?
The following algorithm summarizes all the cases
Let the system of equations be \begin {align*} y & =x1\\ y & =2x+1 \end {align*}
So \[ A=\left ( {\begin {array} [c]{cc}1 & 1\\ 2 & 1 \end {array}} \right ) \] and \[ b=\left ( {\begin {array} [c]{c}1\\ 1 \end {array}} \right ) \]
Mathematica
2
2
The above algorithm can now be run as follows
The output of the above is
Matlab
Output is
Problem: Given \[{\begin {array} [c]{ccc}4x+4y+2z & = & 12\\ 5x+6y+3z & = & 7\\ 9x+7y+10z & = & 9 \end {array}} \]
Automatically convert it to the form \(Ax=b\) and solve
Mathematica

\[ \left ( {\begin {array}{ccc} 4 & 3 & 2 \\ 5 & 6 & 3 \\ 9 & 7 & 10 \\ \end {array}} \right ) \] 
 \[ \{12,7,9\} \] 


Maple

\[ \left [\begin {array}{c} \frac {47}{7} \\ \frac {20}{7} \\ \frac {22}{7} \end {array}\right ] \] 
Problem: Given a matrix A, convert it to REF and RREF. Below shows how to
convert the matrix A to RREF. To convert to REF (TODO). One reason to convert Matrix \(A\) to its row echelon form, is to ﬁnd the rank of \(A\). If matrix \(A\) is a \(4\times 4\), and when converted to its row echelon form we ﬁnd that one of the rows is all zeros, then the rank of \(A\) will be 3 and not full rank.
Mathematica

\[ \left ( {\begin {array}{cccc} 1 & 1 & 2 & 1 \\ 3 & 2 & 4 & 4 \\ 4 & 3 & 3 & 4 \\ \end {array}} \right ) \] 

\[ \left ( {\begin {array}{cccc} 1 & 0 & 0 & 2 \\ 0 & 1 & 0 & 3 \\ 0 & 0 & 1 & 1 \\ \end {array}} \right ) \] 
Matlab


Maple

\[ \left [ {\begin {array}{cccc} 1&0&0&2\\ \noalign {\medskip }0&1&0&3 \\ \noalign {\medskip }0&0&1&1\end {array}} \right ] \] 
Given \begin {align*} A =& \left ( {\begin {array}{cccc} 41 & 45 & 49 & 53 \\ 42 & 46 & 50 & 54 \\ 43 & 47 & 51 & 55 \\ 44 & 48 & 52 & 56 \\ \end {array}} \right ) \end {align*}
Generate the matrix
\[ \left ( {\begin {array}{ccc} 1 & 1 & 41 \\ 2 & 1 & 42 \\ 3 & 1 & 43 \\ 4 & 1 & 44 \\ 1 & 2 & 45 \\ 2 & 2 & 46 \\ 3 & 2 & 47 \\ 4 & 2 & 48 \\ 1 & 3 & 49 \\ 2 & 3 & 50 \\ 3 & 3 & 51 \\ 4 & 3 & 52 \\ 1 & 4 & 53 \\ 2 & 4 & 54 \\ 3 & 4 & 55 \\ 4 & 4 & 56 \\ \end {array}} \right ) \]
Which gives at each row, the location and the value in the original matrix.
Mathematica
Another way
But I think the simplist is to use Table
Matlab
gives
Maple
\[ A= \left [ \begin {array}{cccc} 41&42&43&44\\ \noalign {\medskip }45&46&47&48\\ \noalign {\medskip }49&50&51&52\\ \noalign {\medskip }53&54&55&56 \end {array} \right ] \]
\[ \left [ \begin {array}{ccc} 1&1&41\\ \noalign {\medskip }1&2&45 \\ \noalign {\medskip }1&3&49\\ \noalign {\medskip }1&4&53 \\ \noalign {\medskip }2&1&42\\ \noalign {\medskip }2&2&46 \\ \noalign {\medskip }2&3&50\\ \noalign {\medskip }2&4&54 \\ \noalign {\medskip }3&1&43\\ \noalign {\medskip }3&2&47 \\ \noalign {\medskip }3&3&51\\ \noalign {\medskip }3&4&55 \\ \noalign {\medskip }4&1&44\\ \noalign {\medskip }4&2&48 \\ \noalign {\medskip }4&3&52\\ \noalign {\medskip }4&4&56\end {array} \right ] \]
Given \(A = \{\{1, 0, 9\}, \{5, 0, 6\}, \{4, 1, 9\}, \{7, 0, 11\}, \{8, 1, 2\}\}\)
Find rows with zero in middle, and then remove the zeros from these found.
The result should be \(\{\{1, 9\}, \{5, 6\}, \{7, 11\}\}\)
Mathematica
Many ways to do this. See this
{{1, 9}, {5, 6}, {7, 11}}
Maple
{{1, 9}, {5, 6}, {7, 11}}
Given list \(a = \{1,2,3,4\}\) and list \(b=\{5,6,7,8\}\) how to to call function \(f(x,y)\) by taking \(x,y\) from \(a,b\) one a time so that the result gives \(f(1,5),f(2,6),f(3,7),f(4,8)\)?
Mathematica {f[1, 5], f[2, 6], f[3, 7], f[4, 8]} 
Maple [f(1, 5), f(2, 6), f(3, 7), f(4, 8)]

Given list \(a = \{1,2,3,4\}\) and list \(b=\{5,6,7,8\}\) how to to call function \(f(x,y)\) by taking \(\sin (x),\cos (y)\) from \(a,b\) one a time so that the result gives \[ f(\sin (1),\cos (5)),f(\sin (2),\cos (6)),f(\sin (3),\cos (7)),f(\sin (4),\cos (8)) \]
Mathematica
{f[Sin[1], Cos[5]], f[Sin[2], Cos[6]], f[Sin[3], Cos[7]], f[Sin[4], Cos[8]]}
Maple
[f(sin(1), cos(5)), f(sin(2), cos(6)), f(sin(3), cos(7)), f(sin(4), cos(8))]
Given list \(a = \{1,2,3,4\}\) and list \(b=\{5,6,7,8\}\) how to to call function \(f(2 x+x^2+\sin (x),2+\cos (y))\) by taking \(x,y\) from \(a,b\) one a time so that the result gives \[ \{f(3+\sin (1),5+\cos (5)),f(8+\sin (2),6+\cos (6)),f(15+\sin (3),7+\cos (7)),f(24+\sin (4),8+\cos (8))\} \]
Mathematica
{f[3+Sin[1],5+Cos[5]],f[8+Sin[2],6+Cos[6]],f[15+Sin[3],7+Cos[7]],f[24+Sin[4],8+Cos[8]]}
Maple
In Maple, since it does not have slot numbers # to use, it is better to make a function on the ﬂy, which does the exact same thing.
[f(3 + sin(1), 2 + cos(5)), f(8 + sin(2), 2 + cos(6)), f(15 + sin(3), 2 + cos(7)), f(24 + sin(4), 2 + cos(8))]
Given list of numbers from 1 to 100, select the prime numbers.
Mathematica
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
Maple
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Lets say we want to collect result obtained inside loop, but do not know in advance how many iteration we need.
In Mathematica, Sow and Reap are used. In Maple, an Array can be used or a queue or a table.
Mathematica
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Maple
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Given \(A=[1,2,3,4,5]\) change it to \(A=[5,4,3,2,1]\)
Given \(A=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]\) change it to \[ [2,{\frac {3}{2}},{\frac {5}{3}},{\frac {7}{4}},{\frac {11}{5}},{\frac {13} {6}},{\frac {17}{7}},{\frac {19}{8}},{\frac {23}{9}},{\frac {29}{10}}] \]
\[ \left \{2,\frac {3}{2},\frac {5}{3},\frac {7}{4},\frac {11}{5},\frac {13}{6},\frac {17}{7},\frac {19}{8},\frac {23}{9},\frac {29}{10}\right \} \]
\[ [2,{\frac {3}{2}},{\frac {5}{3}},{\frac {7}{4}},{\frac {11}{5}},{\frac {13} {6}},{\frac {17}{7}},{\frac {19}{8}},{\frac {23}{9}},{\frac {29}{10}}] \]
2.0000 1.5000 1.6667 1.7500 2.2000 2.1667 2.4286 2.3750 2.5556 2.9000
Given set of linearly independent vectors \(V_1=[3,0,0],V_2=[0,1,2],V_3=[0,2,5]\), use GramSchmidt to ﬁnd 3 orthonormal vectors from this set.
\[ \left ( \begin {array}{ccc} 1 & 0 & 0 \\ 0 & \frac {1}{\sqrt {5}} & \frac {2}{\sqrt {5}} \\ 0 & \frac {2}{\sqrt {5}} & \frac {1}{\sqrt {5}} \\ \end {array} \right ) \]
\[ \left [\left [\begin {array}{c} 1 \\ 0 \\ 0 \end {array}\right ], \left [\begin {array}{c} 0 \\ \frac {\sqrt {5}}{5} \\ \frac {2 \sqrt {5}}{5} \end {array}\right ] , \left [\begin {array}{c} 0 \\ \frac {2 \sqrt {5}}{5} \\ \frac {\sqrt {5}}{5} \end {array}\right ] \right ] \]
Problem: Generate one signal of some width and height.
Mathematica


Matlab
Problem: Generate a pulse train of pulses of certain width and height.
Mathematica


Matlab


Given the sequence of numbers \(x(n)=\left [{1,2,3}\right ] \), Find \(X(k) = {\displaystyle \sum \limits _{m=0}^{N1}} x(m) e^{j\frac {2\pi }{N}mk}\) where \(N\) is the length of the data sequence \(x(m)\) and \(k=0 \cdots N1\)