## Matlab License

CUNY offers a campus-wide license to MATLAB, Simulink, and all companion products. All faculty, researchers, and students are eligible to download and install these products on their university computers as well as their personally-owned computers.

### License prerequisites

- You need to have a valid
**CUNY email address**, either in the format citymail or ccny email. - You need to renew license in your Matlab application once a year at the end of November.

### Preparation: Mathworks account

If you don’t have a Mathworks account associated with your **CUNY email address** yet, go to the following URL to create and account:

**URL:** https://www.mathworks.com/login

### Link License

The campus local license administrator will help you to link CUNY site license to your Mathworks account.

Department | License administrator | Phone | |
---|---|---|---|

Levich Institute | Junjun Mao | jmao@ccny.cuny.edu | 212-650-6845 |

Chemical Engineering | Andrew Eng | aeng@ccny.cuny.edu | 212-650-6624 |

Once your local license administrator gives the license activation code, go to your account at Mathworks website and click on link license:

## Installation

### Download the installer

Download the Matlab installer from **Mathworks**.

### Run the installer

When you run the matlab installer, choose installation method with “Log in with a MathWorks Account”.

Then Login with your MathWorks account.

After you login, you will be presented with license choice and please select this license

License | Label | Option |
---|---|---|

1111017 | Standard – Individual |

### Renew the license

The license renewal option is hidden under Help -> Licensing -> Update Current Licenses. You will need to do this every year at the end of November or earl December.

## Servers with preinstalled Matlab

Levich Institute and Chemical Engineering department offer following computing resources that have preinstalled Matlab. Matlab runs best on your own computer but the servers fit situation like:

- One needs to run prolonged sessions of Matlab jobs
- One’s Matlab jobs require memory and CPU more than a personal computer can handle.

### Cheme Linux server

Server Name | cheme.engr.ccny.cuny.edu |
---|---|

Operating system | Ubuntu 18.04 |

CPU | 64 |

RAM | 196 GB |

Remote Access | ssh, vmhub |

Account Application | Junjun Mao <jmao@ccny.cuny.edu> |

### Poincare Linux server

Server Name | poincare.engr.ccny.cuny.edu |
---|---|

Operating system | Ubuntu 10.04 |

CPU | 32 |

RAM | 128 GB |

Remote Access | ssh, vmhub |

Account Application | Junjun Mao <jmao@ccny.cuny.edu> |

Remark | Scheduled to be upgraded soon |

### Chemical Engineering Citrix server

Server Name | cheportal.ccny.cuny.edu |
---|---|

Operating system | Windows |

CPU | 2 |

RAM | 8 GB |

Remote Access | https://cheportal.ccny.cuny.edu |

Account Application | Andrew Eng <aeng@ccny.cuny.edu> |

Remark | This server offers Matlab through non-persistent virtual desktop |

**Note:**

You need City College VPN account to access the server cheme.engr.ccny.cuny.edu and poincare.engr.ccny.cuny.edu by ssh.

## Matlab Online

You can run Matlab Online provided by Mathworks within a browser window if you have a Mathworks account with CCNY site license.

## Learning Matlab

### Online resources

**Matlab Onramp**: Learn the essentials of MATLAB® through this free, two-hour introductory interactive tutorial on commonly used features and workflows.**MIT Open course**: Short videos serve as introduction to Matlab programming for beginners.

### Matlab books

**Physical Modeling in MATLAB**by Allen Downey: learn some programming, some modeling, and some simulation in this free book.

**MATLAB: A Practical Introduction to Programming and Problem Solving (5**by Stormmy Attaway: winner of TAA’s 2017 Textbook Excellence Award (“Texty”).^{th}edition)

## Matlab Quick Reference

### The Matlab Work Environment

### General Commands

**Tip**

Some commands have links to syntax details and examples.

Command | Description |
---|---|

clc | Clear Command Window |

clear | Remove items from workspace, freeing up system memory |

close all | Close all plots |

doc function | Display documentation page in Help browser |

help | Help for functions in Command Window |

ver | Version information for MathWorks products |

% some comment text | Comment text |

CTRL+C | Abort the current operation |

format short | Display 4 decimal places |

format long | Display 15 decimal places |

disp(‘some text’) | Print some text |

### Defining Variables

Command | Description |
---|---|

a = 3 | Define variable a to be 3 |

x = [1, 2, 3] | Set x to be the row vector [1, 2, 3] |

x = [1; 2; 3] | Set x to be the column vector [1, 2, 3]T |

A = [1, 2, 3, 4, 5, 6; 7, 8, 9, 10, 11, 12] | Set A to be a 2 × 6 matrix |

x(2) = 7 | Change x from [1, 2, 3] to [1, 7, 3] |

A(2,1) = 0 | Change A2,1 from 5 to 0 |

### Basic Arithmetic

Command | Description |
---|---|

3*4, 7+4, 2-6, 8/3 | multiply, add, subtract and divide |

3^7 | Compute 3737 |

sqrt(5) | Compute √55 |

log(3) | Compute natural logarithm ln(3)ln(3) |

log10(100) | Compute common logarithm (base 10) log(10)log(10) |

abs(-5) | Compute |-5| |

sin(5*pi/3) | Compute sin(5π/3)sin(5π/3) |

### Round numbers

Command | Description |
---|---|

floor(-1.5) | -2; Round toward negative infinity |

ceil(-1.5) | -1; Round toward positive infinity |

fix(-1.5) | -1; Round toward zero |

round(-1.5) | -2; Round to nearest decimal or integer |

### Matrices and Vectors

Command | Description |
---|---|

zeros(12, 5) | Make a 12 × 5 matrix of zeros |

ones(12, 5) | Make a 12 × 5 matrix of ones |

eye(5) | Make a 5 × 5 identity matrix |

eye(12, 5) | Make a 12 × 5 identity matrix |

linspace(1.4, 6.3, 1004) | Make a vector with 1004 elements evenly spaced between 1.4 and 6.3 |

logspace(1.4, 6.3, 1004) | Make a vector with 1004 elements where the log of the spacing is evenly increasing between 1.4 and 6.3 |

7:15 | Row vector of 7, 8, . . . , 14, 15 |

### Operations on Matrices and Vectors

Command | Description |
---|---|

3 * x | Multiply every element of x by 3 |

x + 2 | Add 2 to every element of x |

x + y | Element-wise addition of two vectors x and y |

A * y | Product of a matrix and vector |

A * B | Product of two matrices |

A .* B | Element-wise product of two matrices |

A ^ 3 | Square matrix A to the third power |

A .^ 3 | Every element of A to the third power |

cos(A) | Compute the cosine of every element of A |

abs(A) | Compute the absolute values of every element of A |

A’ | Transpose of A |

inv(A) | Compute the inverse of A |

det(A) | Compute the determinant of A |

eig(A) | Compute the eigenvalues of A |

size(A) | Get the size of A |

### Slicing Matrices and vectors

Command | Description |
---|---|

x(2:12) | The 2^{nd} to the 12^{th} elements of x |

x(2:end) | The 2^{nd} to the last elements of x |

x(1:3:end) | Every third element of x from the first to last |

A(5,:) | Get the 5^{th} row of A |

A(:,5) | Get the 5^{th} column of A |

A(5, 1:3) | Get the first to third elements in the 5^{th} row |

### Constants

Command | Description |
---|---|

pi | ππ = 3.141592653589793 |

NaN | Not a number (i.e. 0/0) |

Inf | Infinity |

realmax | Largest positive floating-point number 1.7977x103081.7977×10308 |

realmin | Smallest positive floating-point number 2.2251x10−3082.2251×10−308 |

### Loops

**For loop:**

```
for k = 1:5
disp(k);
end
```

**While loop:**

```
k = 0;
while k < 7
k = k + 1;
end
```

### Logicals

```
a = 10
a == 5 % Test if a is equal to 5
false
a == 10 % Test if a is equal to 10
true
a >= 5 % Test if a is greater than or equal to 5
true
a < 11 % Test if a is less than 11
true
a ~= 4 % Test if a is not equal to 4
true
a > 1 && a ~= 10 % Test if a is greater than 1 AND not equal to 10
false
a > 1 || a ~= 10 % Test if a is greater than 1 OR not equal to 10
true
```

### Conditional Statements

```
if a > 10
disp('Greater than 10');
elseif a == 5
disp('a is 5');
else
disp('Neither condition met');
end
```

### Functions

```
function output = addNumbers(x, y)
output = x + y;
end
addNumbers(10, -5)
5
```

### Plotting

Command | Description |
---|---|

plot(x,y) | Plot y versus x (must be the same length) |

loglog(x,y) | Plot y versus x on a log-log scale (both axes have a logarithmic scale) |

semilogx(x, y) | Plot y versus x with x on a log scale |

semilogy(x, y) | Plot y versus x with y on a log scale |

axis equal | Force the x and y axes to be scaled equally |

title(‘A Title’) | Add a title to the plot |

xlabel(‘x label’) | Add a label to the x axis |

ylabel(‘y label’) | Add a label to the y axis |

legend(‘foo’, ‘bar’) | Label 2 curves for the plot |

grid | Add a grid to the plot |

hold on | Multiple plots on single figure |

figure | Start a new plot |

Example:

```
x = linspace(-3*pi, 3*pi, 1000);
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'k-'); % Plot sin(x) as a black line
hold on % Now we can add another curve
plot(x, y2, 'r-'); % Plot cos(x) as a red line
% Set the axis limits
axis([-3*pi, 3*pi, -1.5, 1.5])
% Add axis labels
xlabel('x');
ylabel('y');
% Add a title
title('A plot of cos(x) and sin(x)');
% Add a legend
legend('sin(x)', 'cos(x)');
```