Difference between revisions of "Software tutorial/My first program"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
m
m
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Navigation|Book=Software tutorial|previous=Getting started|current=Tutorial index|next=Loops}}
In this part of the tutorial you are required to create a plot with 10 points (a vector), consisting of the numbers \(x = [0, 2, 4, ... 18] \). Then we will also create a corresponding vector \(y = (x-8)^2 - 40\) and plot these \( (x,y)\) points, pairwise, on a plot.
In this part of the tutorial you are required to create a plot with 10 points (a vector), consisting of the numbers \(x = [0, 2, 4, ... 18] \). Then we will also create a corresponding vector \(y = (x-8)^2 - 40\) and plot these \( (x,y)\) points, pairwise, on a plot.


Line 26: Line 28:
'''Note''': that we must use <tt>.^</tt> to calculate the exponent.  If you just write <tt>^</tt> by itself, MATLAB will assume you want to calculate the matrix exponent, which is defined quite differently.  In fact, any '''element-by-element''' calculation must use the "." notation to tell MATLAB ''not'' to use its default '''matrix''' calculations.
'''Note''': that we must use <tt>.^</tt> to calculate the exponent.  If you just write <tt>^</tt> by itself, MATLAB will assume you want to calculate the matrix exponent, which is defined quite differently.  In fact, any '''element-by-element''' calculation must use the "." notation to tell MATLAB ''not'' to use its default '''matrix''' calculations.


For example, use <tt>.*</tt> for element-by-element matrix multiplication.  If you are bit rusty on this concept, we recommend you read page 4 and 5 of [[Media:MATLAB_primer.pdf | Dr. Mhaskar's Python primer]].
For example, use <tt>.*</tt> for element-by-element matrix multiplication.  If you are bit rusty on this concept, we recommend you read page 4 and 5 of [[Media:MATLAB_primer.pdf | Dr. Mhaskar's MATLAB primer]].


| width="50%" valign="top" |
| width="50%" valign="top" |
Line 33: Line 35:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import numpy as np
import numpy as np
from matlabplotlib.pylab import *
from matplotlib.pylab import *


>>> x = np.array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
>>> x = np.array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
Line 85: Line 87:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import numpy as np
import numpy as np
from matlabplotlib.pylab import *
from matplotlib.pylab import *
</syntaxhighlight>
</syntaxhighlight>
''Advanced topic:''The reason for <tt>import</tt> is due to a technical issue related to [http://bytebaker.com/2008/07/30/python-namespaces/ Python namespaces] -->
''Advanced topic:''The reason for <tt>import</tt> is due to a technical issue related to [http://bytebaker.com/2008/07/30/python-namespaces/ Python namespaces] -->

Latest revision as of 21:12, 7 February 2017

← Getting started (previous step) Tutorial index Next step: Loops →

In this part of the tutorial you are required to create a plot with 10 points (a vector), consisting of the numbers \(x = [0, 2, 4, ... 18] \). Then we will also create a corresponding vector \(y = (x-8)^2 - 40\) and plot these \( (x,y)\) points, pairwise, on a plot.

MATLAB Python

There are several ways we can create our vector \(x\) in MATLAB.

>> x = [0 2 4 6 8 10 12 14 16 18];
>> x = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18];
>> x = linspace(0, 18, 10); % easiest method
>> x
x =
     0     2     4     6     8    10    12    14    16    18

Now create the corresponding \(y\)-vector using MATLAB's ability to do vector operations. We can write

>> y = (x-8).^2 - 40;
>> y
y =
    24    -4   -24   -36   -40   -36   -24    -4    24    60

Note: that we must use .^ to calculate the exponent. If you just write ^ by itself, MATLAB will assume you want to calculate the matrix exponent, which is defined quite differently. In fact, any element-by-element calculation must use the "." notation to tell MATLAB not to use its default matrix calculations.

For example, use .* for element-by-element matrix multiplication. If you are bit rusty on this concept, we recommend you read page 4 and 5 of Dr. Mhaskar's MATLAB primer.

There are several ways we can create our vector \(x\) in Python.

import numpy as np
from matplotlib.pylab import *

>>> x = np.array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
>>> x = np.linspace(0, 18, 10)   # easiest method
>>> x
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

Now create the corresponding \(y\)-vector:

>>> y = (x-8) ** 2 - 40  
>>> y
array([ 24.,  -4., -24., -36., -40., -36., -24.,  -4.,  24.,  60.])

We raise a variable to a power using the ** notation; the ^ operator means something else in Python.

Key difference

  • In MATLAB, everything is a matrix calculation, by default.
  • In Python's NumPy library (numpy), everything is an element-by-element calculation, by default.


Finally, we are ready to plot these \( (x,y)\) points. Notice that the code is nearly identical.

MATLAB Python
plot(x, y, '.-')
grid on
xlabel('x')
ylabel('y')
title('A plot of y = (x-8)^2 - 40')
borderless
plot(x, y, '.-')
grid('on')  # <--- it is a function in Python, so add ()
xlabel('x')
ylabel('y')
title('A plot of y = (x-8)^2 - 40')
borderless