Mathematica

Final notes for ECE1254, Modelling of Multiphysics Systems

December 27, 2014 ece1254 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Capture

I’ve now finished my first grad course, Modelling of Multiphysics Systems, taught by Prof Piero Triverio.

I’ve posted notes for lectures and other material as I was taking the course, but now have an aggregated set of notes for the whole course posted.
This is now updated with all my notes from the lectures, solved problems, additional notes on auxillary topics I wanted to explore (like SVD), plus the notes from the Harmonic Balance report that Mike and I will be presenting in January.

This version of my notes also includes all the matlab figures regenerating using http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig, which allows a save-as pdf, which rescales much better than Matlab saveas() png’s when embedded in latex.  I’m not sure if that’s the best way to include Matlab figures in latex, but they are at least not fuzzy looking now.

All in all, I’m pretty pleased with my notes for this course.  They are a lot more readable than any of the ones I’ve done for the physics undergrad courses I was taking (https://peeterjoot.com/writing/).  While there was quite a lot covered in this course, the material really only requires an introductory circuits course and some basic math (linear algebra and intro calculus), so is pretty accessible.

This was a fun course.  I recall, back in ancient times when I was a first year student, being unsatisfied with all the ad-hoc strategies we used to solve circuits problems.  This finally answers the questions of how to tackle things more systematically.

Here’s the contents outline for these notes:

Preface
Lecture notes
1 nodal analysis
1.1 In slides
1.2 Mechanical structures example
1.3 Assembling system equations automatically. Node/branch method
1.4 Nodal Analysis
1.5 Modified nodal analysis (MNA)
2 solving large systems
2.1 Gaussian elimination
2.2 LU decomposition
2.3 Problems
3 numerical errors and conditioning
3.1 Strict diagonal dominance
3.2 Exploring uniqueness and existence
3.3 Perturbation and norms
3.4 Matrix norm
4 singular value decomposition, and conditioning number
4.1 Singular value decomposition
4.2 Conditioning number
5 sparse factorization
5.1 Fill ins
5.2 Markowitz product
5.3 Markowitz reordering
5.4 Graph representation
6 gradient methods
6.1 Summary of factorization costs
6.2 Iterative methods
6.3 Gradient method
6.4 Recap: Summary of Gradient method
6.5 Conjugate gradient method
6.6 Full Algorithm
6.7 Order analysis
6.8 Conjugate gradient convergence
6.9 Gershgorin circle theorem
6.10 Preconditioning
6.11 Symmetric preconditioning
6.12 Preconditioned conjugate gradient
6.13 Problems
7 solution of nonlinear systems
7.1 Nonlinear systems
7.2 Richardson and Linear Convergence
7.3 Newton’s method
7.4 Solution of N nonlinear equations in N unknowns
7.5 Multivariable Newton’s iteration
7.6 Automatic assembly of equations for nonlinear system
7.7 Damped Newton’s method
7.8 Continuation parameters
7.9 Singular Jacobians
7.10 Struts and Joints, Node branch formulation
7.11 Problems
8 time dependent systems
8.1 Assembling equations automatically for dynamical systems
8.2 Numerical solution of differential equations
8.3 Forward Euler method
8.4 Backward Euler method
8.5 Trapezoidal rule (TR)
8.6 Nonlinear differential equations
8.7 Analysis, accuracy and stability (Dt ! 0)
8.8 Residual for LMS methods
8.9 Global error estimate
8.10 Stability
8.11 Stability (continued)
8.12 Problems
9 model order reduction
9.1 Model order reduction
9.2 Moment matching
9.3 Model order reduction (cont).
9.4 Moment matching
9.5 Truncated Balanced Realization (1000 ft overview)
9.6 Problems
Final report
10 harmonic balance
10.1 Abstract
10.2 Introduction
10.2.1 Modifications to the netlist syntax
10.3 Background
10.3.1 Discrete Fourier Transform
10.3.2 Harmonic Balance equations
10.3.3 Frequency domain representation of MNA equations
10.3.4 Example. RC circuit with a diode.
10.3.5 Jacobian
10.3.6 Newton’s method solution
10.3.7 Alternative handling of the non-linear currents and Jacobians
10.4 Results
10.4.1 Low pass filter
10.4.2 Half wave rectifier
10.4.3 AC to DC conversion
10.4.4 Bridge rectifier
10.4.5 Cpu time and error vs N
10.4.6 Taylor series non-linearities
10.4.7 Stiff systems
10.5 Conclusion
10.6 Appendices
10.6.1 Discrete Fourier Transform inversion
Appendices
a singular value decomposition
b basic theorems and definitions
c norton equivalents
d stability of discretized linear differential equations
e laplace transform refresher
f discrete fourier transform
g harmonic balance, rough notes
g.1 Block matrix form, with physical parameter ordering
g.2 Block matrix form, with frequency ordering
g.3 Representing the linear sources
g.4 Representing non-linear sources
g.5 Newton’s method
g.6 A matrix formulation of Harmonic Balance non-linear currents
h matlab notebooks
i mathematica notebooks
Index
Bibliography

Pick a number between 1 and 10

July 14, 2014 math and physics play , , , ,

I saw the following on mathfail.com (EDIT: dead link), and thought about it a bit

 

Notice that the +4 here is entirely misdirection.  This is really just a statement that the sum of the digits of any integer power of 9 up to 81, is 9.

It also appears to be true that, for integer a in \([1, N+1]\)

\[(N a) \text{div} (N+1) + (N a) \text{mod} (N + 1) = N.\]

This is demonstrated in the following Mathematica Manipulate

However, I’m unsure how to prove or disprove this?

How much difference will shopping around for mortgage rates make?

June 17, 2014 Incoherent ramblings , , , , , , , ,

[Click here for a standalone PDF of this post]

Motivation

The banks are all offering variable rate mortgages at slight differences from prime (current 3%). I have asked for a few competing rate quotes to see which is best.

Scotiabank offered \(\text{prime} – 0.53\%\). PC Financial offered \(\text{prime} – 0.55\%\), slightly better. ING’s offer wasn’t competitive at \(\text{prime} – 0.25\%\). BMO offered the best so far at \(\text{prime} – 0.6\%\), with the conditions that we’d have to open joint chequing and a joint BMO mastercard. Royal’s default offering matched PC Financial’s offer at \(\text{prime} – 0.55\%\), and she’s now playing the back room manager game to see if she can get approval to beat (or even match) BMO’s offer.

Let’s compare those here and see how much difference these quotes result in. Is it even worth it to do this negotiation?

Guts

Consider first a principle amount \(-P\), and set of payments \(A, B, C, D, …\), equally spaced in time, corresponding with some effective interest rate per period. This is sketched in fig 1.1.

Fig 1.1: Payments at fixed intervals

Fig 1.1: Payments at fixed intervals

 

We want to refresh our memory about future value calculations for such a set of payments. Suppose the interest rate per period is \(i\), for example \(i = 0.03\) for a 3% rate, then at the first, second, third, and fourth intervals, we have respectively

\begin{equation}\label{eqn:mortgageInterest:20}
\begin{aligned}
-P(1 + i) + A & \\
\lr{ -P(1 + i) + A }(1 + i) + B &= -P(1+i)^2 + A(1 + i) + B \\
\lr{ \lr{ -P(1 + i) + A }(1 + i) + B}(1 + i) + C &= -P(1+i)^3 + A(1 + i)^2 + B(1 + i) + C \\
\lr{ \lr{ \lr{ -P(1 + i) + A }(1 + i) + B}(1 + i) + C}(1 + i) + D
&= -P(1+i)^4 + A(1 + i)^3 + B(1 + i)^2 + C(1+i) + D.
\end{aligned}
\end{equation}

We can treat the payments independently, each with a separate \((1+i)^k\) factor adjusting the future value of that payment. The case where the payments are of equal value is of particular interest. For that, after \(k\) payments, the future value of the initial principle offset by any of the payments is

\begin{equation}\label{eqn:mortgageInterest:40}
F_k
= -P(1+i)^k + A(1 + i)^{k-1} + A(1 + i)^{k-2} + \cdots A.
\end{equation}

Recall that a geometric sum

\begin{equation}\label{eqn:mortgageInterest:60}
S_k = 1 + a + a^2 + \cdots + a^{k-1},
\end{equation}

can be solved by writing

\begin{equation}\label{eqn:mortgageInterest:80}
a S_k – S_k = a^k – 1,
\end{equation}

so that

\begin{equation}\label{eqn:mortgageInterest:100}
S_k = \frac{a^k – 1}{a – 1}.
\end{equation}

The future value thus sums to

\begin{equation}\label{eqn:mortgageInterest:120}
F_k
=
-P(1+i)^k + A \frac{ (1 + i)^k – 1}{ 1 + i – 1 },
\end{equation}

or
\begin{equation}\label{eqn:mortgageInterest:160}
\boxed{
F_k
=
\lr{ -P + \frac{A}{i} } (1 + i)^k – \frac{A}{i}.
}
\end{equation}

Observe that we can invert \ref{eqn:mortgageInterest:160} for \(k\) by taking logs of the inequality \(F_k \ge 0\). This gives

\begin{equation}\label{eqn:mortgageInterest:180}
\boxed{
k \ge – \frac{\ln\lr{ 1 – i P/A}}{\ln\lr{1 + i}}
}
\end{equation}

It’s clear that we are in trouble (with always negative future value, and no solution to the inequality) unless \(-P + A/i > 0\), or

\begin{equation}\label{eqn:mortgageInterest:140}
A > i P.
\end{equation}

For example, at \(i = 3\%\) interest per year, compounded monthly, the monthly break even payment rate for various mortgage amounts \(P\) is tabulated in table 1.1

This provides a first hint that the 0.5-0.6 less than prime rates that the various banks offer will make a difference. For a principle of \(200 K\), we require \(17\) dollars more per month to break even (not paying down the principle at all) when comparing prime less \(0.5\%\) and \(0.6\%\).

Suppose we make \(1000\) per month payments at prime minus various amounts. At the end of a 5 year (60 month) term, we have the following future values

With a mortgage amount of 225K and the nominal monthly payment amount of 1K, is this negotiation worth the time? It appears that finding somebody willing to loan at prime minus 0.6% vs. prime minus 0.5% is only worth about $550 after five years. That saving amounts to about 1.5 months of the banks interest profit.

It’s interesting to see that, despite the bank making on the order of 25K for such a mortgage after five years, how little they are willing to move in their adjustment of interest rates.

If you have the wolfram CDF plugin installed, equations \ref{eqn:mortgageInterest:120}, and \ref{eqn:mortgageInterest:160} can be executed using the following simple monthly compounding rate calculator app

 

Peeter Joot’s new blog: more to come…

May 21, 2014 Incoherent ramblings , , , , , , , , , ,

After 611 blog posts on my old wordpress.com hosted blog, dating all the way back to 2009, I’ve decided to ante-up and pay for more flexible hosting.

My primary motivation for this was truly geeky. I wanted the flexibility to be able to manage wordpress plugins (i.e. mathjax-latex and wolframcdf), and to also be able to put plain old html and arbitrary file content into the apache2 directory structure. I’ve wanted plain html hosting for a while, but made do with google sites (i.e. crappy but free). I’d also wanted to be able to use the wolfram CDF plugin on my blog, but also not enough to pay for it. However, once I tried mathjax-latex, I was sold. Compared to wp-latex, this “new way” completely kicks ass, and should save me a lot of time.

I tried out an amazon EC2 bitnami image for a while (amazon offers a free trial year to evaluate their offerings). That’s a flexible setup and offers direct access to the Linux VM, which is very nice. However, with an amazon EC2 image, I’m not really sure what I would end up paying. The charts seem somewhat vague, depending on future usage of both machine and storage. I would also have pay separately for a domain name, and pay separately for amazon hosting of the DNS entry.

I ended up deciding to use a go-daddy hosted wordpress instance, which is a flat rate service. It is less flexible than a godaddy standalone web-hosting environment, but also cheaper ($12 for the first year, including the domain name, and ~$50/year after that). It also looks like I can upgrade this to a more generic web hosting environment later if the cost of that seems justified. I’ll see first if only having sftp access to htdocs is enough of a major inconvenience to pay that additional yearly fee. If not, then I may consider changing to another host.

Configuring a custom MathJax configuration was a bit of a pain with only sftp access, mostly because I had to copy the MathJax tree, which was very slow for so many small files. I did that directory tree transfer with FileZilla since sftp ‘put –r’ appears to be busted. This MathJax setup was way easier on the EC2 since the ssh shell allowed for wget and local unzip directly from the apache2 htdocs tree. It’s a shame that the mathjax-latex plugin doesn’t allow the MathJax tree to be served from the default server (what the plugin settings calls the ‘MathJax CDN Service’). Logically, I’d like to be able to use that CDN service, but have my configuration file hosted locally. That config file (config/default.js) is a single small file, and is likely all that I’ll ever have to alter in that whole directory tree.

I haven’t decided whether or not I’ll keep my old peeterjoot.wordpress.com blog, or switch unconditionally to this new peeterjoot.com blog (which will be the new home for any of my mathematical or physics related posts). This new blog has no blog-article content so far, and doesn’t yet have a theme template that I like. What is here so far is:

  • An enumeration of things I have written, including archives of all the individual pdfs that I have posted over the years along with my blog entries. All these pdfs are now stored directly on the new site in the htdocs tree. I will no longer be using any of my (three) old google sites pages as pdf stores.
  • A chronological listing of all the Mathematica notebooks I have written. The newest versions of these notebooks can still be found in my Mathematica github repository. A snapshot of each of these is now also available on the new site, so if you have the CDF plugin installed, these can now be examined by clicking on the links directly. Ironically, with chrome and my CDF installation, I’m able to view the .nb suffixed notebooks directly in the browser, but a click on any CDF (.cdf) notebook triggers a download?
  • I’ve made a couple notes about my setup of the mathjax-latex plugin, and the differences in latex markup with that plugin compared to the wp-latex plugin (which is available by default on wordpress.com). My future mathematical blogging should be way easier, probably won’t require any of my old tex2blog script, and will also look better!
  • An About page, copied directly from the About page on my old blog.

More to come, … now that I’ve finally finished the Stokes theorem chapter in my Geometric Algebra compilation, I expect new posts to be more frequent.