Sunday, January 24, 2010

Mortgage calculator - bash script

This simply outputs the monthly mortgage payment.

Takes five arguments : principle years interest points fees

For instance,

bash mortgage.sh 100000 5 .075 0 0

Outputs

Principle + fees = 100000 , Years = 5 , Months = 60 , interest .006250000000
Points = 0 , 0
2003.794859563881




#!/bin/sh

if [[ $# -ne 5 ]]
then
echo "enter 5 args : principle years interest points fees"
exit
fi

prin=$1
((mon=$2 * 12 ))
inter=`echo "scale=12;$3/12" | bc`
pts=`echo "scale=12; $prin * $4" | bc`
fee=$5
prin=`echo "scale=12;$prin + $fee" | bc`


echo "Principle + fees = $prin , Years = $2 , Months = $mon , interest $inter"
echo "Points = $4 , $pts"

prin=`echo "scale=12; $prin - $pts" | bc`


echo `echo "scale=12;($prin*$inter)*((1+$inter)^$mon)/(((1+$inter)^$mon)-1)"|bc`
echo "the monthly payment : $new_prin"



Sunday, January 17, 2010

start menu pin list location



> If I pin a shortcut to the startmenu, where does XP store this
> information?

With a shortcut in this folder...
C:\Documents and Settings\All Users\Start Menu

The above folder would be the easiest.

Also here...
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\
Explorer\MenuOrder\Start Menu2\Programs
Value Name: Order

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\
Explorer\StartPage
Value Name: Favorites
Value Name: FavoritesResolve

All of those are REG_BINARY values and hard to read them.

Monday, January 11, 2010

Bash - Gaussian Elimination Op count example



#!/bin/sh

if [[ $# -ne 2 ]]
then
exit
fi

echo " m $1 by n $2 matrix"

n=$2
m=$1
mult=0
add=0
c=0

for ((i=1;i < m;i++))
do
echo "row = i = $i"
for ((j=i;j < m;j++))
do
((mult=mult + (n-i+1)))
((add=add + (n-i+1)))
done
((c=c+(m-i)*(n-i+1)))
done
echo "additions = $add"
echo "mults = $mult"
echo "summation = $c"



Matlab - simple newton's method ex



function r = doNewton(f,x0,x,err)

while abs(x0 - x) > err

x0, (x0 - x)

x0 = x0 - (polyval(f,x0))/polyval(polyder(f),x0);


end



r = x0;
end

Saturday, November 7, 2009

Jacobi Method example

I wrote this code a while back. Apparently, this post is getting a few hits. I refined it a bit. Also wanted to make sure it was looked right.

What is the Jacobi method? It's an iterative way of solving a system of equations. Ax = b. This is learned in Numerical Analysis classes.

Generally, you decompose a matrix into its diagonal and lower and upper triangles. Such that,

A = D + L + U

x0 is the initial guess.

Now you want to solve for x. x_approx = inverse of D * (b - (L+U)x0)

Get x_approx from this formula, then plug it into x0 and do it all over again until you reach the desired error.



% Code is for matlab / octave
format long e;
A = [ 2 -1; -1 2];
b = [ 2; 1];
k = 20;
xi = [0;0]; % initial guess

%we want
%x' = Dinv(b - (L+U)x)

% just take the diagonal of A
D=[A(1,1) 0;0 A(2,2)];

Di=inv(D);

%lower and upper triangles of A
L=[0 0; -A(2,1) 0];
U=[0 -A(1, 2); 0 0];

% this part doesn't change, so calc now
C=Di*b;
% this is the main term of the algo
M=Di*(L+U);

x = [5/3; 4/3]; % solution

abs_err = [ 100 ; 100];
abs_err = abs_err - x;
i=0;

%stop until error is <= 1e-6
while abs_err(1) >= 1e-6 && abs_err(2) >= 1e-6
xi=M*xi+C
abs_err = abs(xi - x);
fprintf('i = %f abs-err = %f %f \n',i,abs_err(1),abs_err(2));
i=i+1;
end


Friday, November 6, 2009

matlab : polynomial fitting



octave-3.2.3:4> for i = 1 : 51
> x(i) = .2*(i - 1);
> end


octave-3.2.3:6> for i = 1: 51
> y(i) = 1 - x(i);
> end

octave-3.2.3:8> polyfit(x,y,4)
ans =

1.1249e-17 -2.3654e-16 1.7285e-15 -1.0000e+00 1.0000e+00

Wednesday, November 4, 2009

Java : sample applet






import java.applet.*;
import java.awt.*;

public class A extends Applet
{
private int w, h;
public void init( )
{
w = 45;
h = 50;
}

public void paint(Graphics g)
{
g.drawRect(w, h, 20, 80);
}
}