%% Chapter file for Chapter 6, Programming
clear all
%% Branching with if
absval(2)
absval(-3)
absval2(4)
absval2(-5.6)
signum(1.2),signum(-pi),signum(0)

%% Logical expressions
help relop

(1 > 0) | (-1 > 0)
2 > 3
2 < 3

help logical
 
2 | 3
[2 3] < [3 2]

x = -2:2; x >= 0
x (x >= 0)
x(3:5)

%% branching and logical expressions
% These commands show how f1 works properly with scalars, 
% but not with vectors.
f1(pi/2)
f1(pi)
f1([pi/2 pi])
f1([0 1])

% These commands show how f1a can cope with vector input
% but zero is still a problem.
f1a([1 2 3 4])
f1a([0 0])
f1a([0 pi 2])

% f2 works better.
f2([1 2 3 4])
f2([0 0])
f2([0 pi 2])

% f3 and f4 also work fine.
f3([1 2 3 4])
f3([0 0])
f3([0 pi 2])

f4([1 2 3 4])
f4([0 0])
f4([0 pi 2])

%% Branching with switch
count(1)
count(2)
count(3)
count([1 2])

%% more about loops
SumSeriesViaWhile

SumSeriesViaBreak

%% subfunctions
 x = [1 2 3 4 5]
 y = [ 0 -2 -4 6]
 z = [ 5 7 -3]
sumcuberoots(x)
sumcuberoots(y)
sumcuberoots(z)

%% Parsing input and output
add1(4, 5)
add1(4, 5, 6)
add1(7)
%%
add1(8,9,10,11)
%%
add2([4, 5])
add2([4, 5, 6])
add2(7)
add2([8,9,10,11])
%%
add3([8,9,10,11])
add3([4, pi])
add3([4, '5', 6])

%%
[x, y] = rectangular1(2, 1)
[x, y] = rectangular1(5, pi/4)
rectangular1(5, pi/2)
rectangular1(5, pi/2, 0)

%%
[x, y] = rectangular2(5, pi/4)
rectangular2(5, pi/4)

%% Evaluation and function handles
add4 = @(x, y, z) eval('x + y + z', 'x + y')
add4(4,5)
add4(4,5,6)
add4(1)
%%
add4(4,5,6,7)
%%
rectangular3 = @(r, theta) deal(r.*cos(theta), r.*sin(theta))
[x, y] = rectangular3(5, pi/4)
z = rectangular3(5, pi/4)

%%
iterate(@cos, 1, 2)
iterate(@cos, 1, 100)
%%
iterate(@(x) 3.9*x*(1-x), 0.5, 100)
%%
for c = 1:3
    hold on
    ezplot(@(x) sin(c*x), [0 2*pi])
end
hold off
