Saturday, April 16, 2016

Basic 2D, 3D Function Plotting in Matlab

There are a number of ways you can plot a function in matlab. Let's see some examples:

1. Let's start with one variable. Plot of the function $f(x) = x^2 -3$ would be:
fplot(@(x) x^2 - 3, [-5 5]) //fplot requires limits

2. Plotting the sum of the outcomes of two dice throw:
We know that the outcome of a dice throw is uniformly distributed over the range [1,6]. So the sum of the outcomes should be in the range [2, 12]. If we continue this experiment many times, we will start to notice that the output is not uniformly distributed, it's actually normal. We can see the simulation by running the command :
hist(randi([1 6], 1, 10000)+randi([1 6], 1, 10000))%adding two uniformly distributed r.v of the range(1,6) 10000 times
Which should produce an image resembling a normal distribution:

2. Drawing a circle from the equation ( $x^2 + y^2 = 5^2 $):
f = @(x) sqrt(5^2 - x.^2)
fplot(@(x) sqrt(5^2 - x.^2), [-5 5])
hold on
fplot(@(x) (-1)*sqrt(5^2 - x.^2), [-5 5])
hold off
axis equal %to preserves axis ratio, else would end up looking like an ellipse 


3. Let's consider a function $z = x^2 + y^2$, for a range of values of x and y:
[x, y] = meshgrid(linspace(-10, 10, 100), linspace(-10, 10, 100))
z = x.^2 + y.^2
mesh(x,y,z)


4. Another example:
[x, y] = meshgrid(linspace(-10, 10, 100), linspace(-10, 10, 100))
xcosx = x.*cos(x)
ysiny = y.*sin(y)
z = xcosx' * ysiny
surfc(x, y , z)

Friday, April 15, 2016

Interesting Plots

1. General shape of an even element Magic Square:

plot(magic(16))
2. General shape of an odd element Magic Square:
plot(magic(15))
3.
x = -100:100;
y = -100:100;
[X, Y] = meshgrid(x, y);
figure
surf(x, y, X./Y)



4.  //2 b contin

Tuesday, April 12, 2016

Solving a simple Linear Regression problem using CVX and Matlab

Caution: Contains over-(simplification, generalization, and some other *tions)

Homer is a hawker who sells batman toys. Kids love those very much and his business is on the rise. He noticed that for the past five days, these are his costs and profits:

Items Sold Profit
100 960
80 760
70 660
150 1460
90 860

Now, a competitor, Ned, has stolen these data and wants to know a little more about his daily operating expense and per item price. Ned is a very good statistician and feels that these data should form a nice regression problem expressed in the following equation:

y = b0 + b1*x

Where 'y' is the value of profit, 'b0' is the fixed cost that Homer incurs everyday and 'b1' is the per price item and 'x' is the number of items Homer sells everyday.

Ned recently bought a license of Matlab and downloaded cvx, a nice modelling tool and thought to give it a try to find out Homer's secrets.

He writes the following matlab code:
b = [960 760 660 1460 860]; % profit vector
x = [100 80 70 150 90]; % item sold each day

cvx_begin
    variables b1 b0 % b1 = per item price, b0 = fixed cost per day
    minimize(norm(((b1*x) + b0) - b )) % minimizing the estimated and actual values
cvx_end
And yes, he finds that per day cost is 40 and per item price is 10. (For Algebraist: ya, I know)