%% Commands for Chapter 2, Matlab Basics

%% Initialization

clear all
close all
format short
set(0, 'DefaultAxesFontSize', 16)

%% 2.1 Input and Output
1/2 + 1/3

ezplot('x^3 - x')

%% 2.2 Arithmetic
3^2 - (5 + 4)/2 + 6*3

ans^2 + sqrt(ans)

u = cos(10)
v = sin(10)
u^2 + v^2

%% 2.3 Recovering from Problems

%% 2.3.1 Errors in Input
% Try the following in the Command Window; syntax error prevents script
% from running even with try/catch wrapper.

%3u^2

%% 2.3.2 Aborting Calculations

%% 2.3.3 Online Help

%% 2.4 Algebra or Symbolic Computation
syms x y
(x - y)*(x - y)^2
expand(ans)
factor(ans)

simplify((x^3 - y^3)/(x - y))

simplify((sin(x) + cos(x))^2)
simple((sin(x) + cos(x))^2)

%% 2.4.1 Substituting in Symbolic Expressions
d = 1, syms u v
w = u^2 - v^2
subs(w, u, 2)
subs(w, v, d)
subs(w, v, u + v)
simplify(ans)

%% 2.4.2 Symbolic Data, Variable Precision, and Exact Arithmetic
cos(pi/2)
cos(sym('pi/2'))
sym('1/2') + sym('1/3')
vpa('sqrt(2)', 50)

3^45
vpa(3^45)
vpa('3^45')

%% 2.5 Vectors and Matrices

%% 2.5.1 Vectors
Z = [2,4,6,8]
Y = [4 -3 5 -2 8 1]
X = 1:9

X = 0:2:10
X(3)
X'

X.^2
X.*Y
exp(X)

%% 2.5.2 Matrices
A=[1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]
A*Z'

%% 2.6 Suppressing Output
X = -1:0.1:2;

%% 2.7 Functions

%% 2.7.1 Built-in Functions
log(exp(3))
sin(2*pi/3)
sin(sym('2*pi/3'))

%% 2.7.2 User-Defined Functions
f = @(x) x^2
f(4)
f = @(x) x.^2
f(1:5)
g = @(x, y) x.^2 + y.^2; g(1, 2)
g([1 2], [3 4])

%% 2.8 Managing Variables

a = pi
b = 'pi'
c = sym('pi')
z = sqrt(-1)

%%
whos

%%
try
    sin(b)
catch err
    disp(err.message)
end

%%
2*b

%% 2.8.1 Naming and Clearing Variables

sec = 60

%%
sec(1)

%%
try
    sec(pi)
catch err
    disp(err.message)
end

%% 2.9 Solving Equations

syms x; solve(x^2 - 2*x - 4 == 0)
double(ans)

%%
solve(x^2 - 2*x - 4)

%%
solve('x^2 = 3*x - 7')
double(ans)

%%
syms y; solve(2*x - log(y) == 1, y)

%%
[xsol, ysol] = solve(x^2 - y == 2, y - 2*x == 5)
[xsol(1) ysol(1)]

%%
sol = solve(x^2 - y == 2, y - 2*x == 5)
sol.x
sol.y

%%
solve(sin(x) == 2 - x)

%%
digits(10); solve(log10(x) == cos(x))
log10(ans) - cos(ans)

%%
solve(log10(x) == cos(x), 'Real', true)
syms x real; solve(log10(x) == cos(x))

%%
fzero(@(x) log10(x) - cos(x), 2)
fzero(@(x) log10(x) - cos(x), 7)

%%
syms x clear, assume(0 < x < 2), solve(log10(x) == cos(x))
assume(x > 6), solve(log10(x) == cos(x))

%% 2.10 Graphics

%% 2.10.1 Graphing with ezplot
ezplot('x^2 + x + 3', [-2 2])

print -deps matbasics2.eps

figure
syms x; ezplot(x^2 + x + 3, [-2 2])
figure
ezplot(@(x) x.^2 + x + 3, [-2 2])
figure
ezplot('x^2 + x + 5', [-2 2])

%% 2.10.2 Modifying Graphs

axis([-2 2 0 10])

%%
title 'A Parabola'

%%
axis square

%%
axis equal

%%
axis tight

%% 2.10.3 Graphing with plot
close all
X = [1 2 3]; Y = [4 6 5]; plot(X,Y)

print -deps matbasics3.eps
figure
X = -2:0.01:2;
plot(X, X.^2 + X + 3)

%print -deps matbasics4.eps

%% 2.10.4 Plotting Multiple Curves
close all
ezplot(@log10, [0 10])
hold on
ezplot(@cos, [0 10])
hold off
title 'log10(x) and cos(x)'

print -deps matbasics1.eps

figure
X = 0:0.01:10;
plot(X, log10(X), X, cos(X))


