In [1]:
#you must do this to enable Matlab in a non-matlab notebook
%load_ext pymatbridge
In [2]:
#Python
import numpy as np
b = 4
x = np.linspace(0,10, 5)
y = x * b
print(y)
In [3]:
%%matlab
b = 4;
x = linspace(0,10,5);
y = b .* x
There are three main differences we can spot:
;
at the end to suppress output. If you don't end with a ;
, Matlab will tell you about that line. numpy
, all arithmetic operations are by default element-by-element. In Matlab, arithmetic operations are by default their matrix versions. So you put a .
to indicate element-by-element
In [4]:
x = np.array([[4,3], [-2, 1]])
y = np.array([2,6]).transpose()
print(x.dot(y))
In [5]:
%%matlab
x = [4, 3; -2, 1];
y = [2,6]';
x * y
You can see here that Matlab doesn't distinguish between lists, which can grow/shrink, and arrays, which are fixed size
In [6]:
x = [2,5]
x.append(3)
x
Out[6]:
In [7]:
%%matlab
x = [5,2];
x = [x 3]
Since Matlab variables are always fixed length, you must create new ones to to change size
Many of the same commands we used have the same name in matlab
In [25]:
import scipy.linalg as lin
example = np.random.random( (3,3) )
lin.eig(example)
Out[25]:
In [27]:
%%matlab
example = rand(3,3);
eig(example)
In [8]:
%%matlab
x = 1:10;
%this is how you make comments BTW, the % sign
x(1)
x(1:2)
x(1:2:5)
In [9]:
x = list(range(1,11))
print(x[0])
print(x[0:2])
print(x[0:6:2])
In [10]:
for i in range(3):
print(i)
print('now with a list')
x = [2,5]
for j in x:
print(j)
Matlab can only iterate in for loops on integers. Thus, to iterate over elements of an array, you need use this syntax:
In [11]:
%%matlab
for i = 0:2
i
end
'now with a list'
x = [2, 5]
n = size(x)
for j = 1:n
j
end
If statements are similar. and
is replaced by &
, or
by |
and not
by ~
In [12]:
%%matlab
a = -3;
if a < 0 & abs(a) > 2
a * 2
end
if ~(a == 3 | a ~= 3)
'foo'
end
In [13]:
%%writefile compute_pow.m
function[result] = compute_pow(x, p)
%this function computes x^p
result = x ^ p;
In [14]:
%%matlab
compute_pow(4,2)
If you modify the file, you have to force matlab to reload your function:
In [15]:
%%matlab
clear compute_pow
In [21]:
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0,10,100)
y = np.cos(x)
plt.figure(figsize=(7,5))
plt.plot(x,y)
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.title('cosine wave')
plt.show()
In [20]:
%%matlab
x = linspace(0,10,100);
y = cos(x);
plot(x,y)
grid on
xlabel('x')
ylabel('y')
title('cosine wave')
In [48]:
from scipy.optimize import *
def fxn(x):
return (x - 4)**2
x_min = newton(fxn,x0=0)
plt.figure(figsize=(7,5))
x_grid = np.linspace(2,6,100)
plt.plot(x_grid, fxn(x_grid))
plt.axvline(x_min, color='red')
plt.show()
In [39]:
%%writefile my_obj.m
function[y] = my_obj(x)
%be careful to use .^ here so that we can pass matrices to this method
y = (x - 4).^2;
In [47]:
%%matlab
[x_min, fval] = fminsearch(@my_obj, 0);
x_grid = linspace(2,6,100);
plot(x_grid, my_obj(x_grid));
hold on
ylimits=get(gca,'ylim');
plot([x_min, x_min], ylimits)
There are a few key differences to note:
@
if you're not calling itExcel is a pretty self-explanatory program. I'm just going to show you some advanced things which you may not already know.
In [12]:
import pandas as pd
data = pd.read_excel('fuel_cell.xlsx')
data.info()
You can access data in two ways:
In [10]:
data.Resistance[0:10]
Out[10]:
In [11]:
data.ix[0:10, 3]
Out[11]:
In [ ]:
%system jupyter nbconvert unit_10_lecture_1.ipynb --to slides --post serve
In [ ]: