%Mfile for Chapter 2: Matlab Basics
delete chap2.txt
diary chap2.txt
echo on
clear all

1/2 + 1/3
ezplot('x^3 - x')
pause

3^2 - (5 + 4)/2 + 6*3
ans^2 + sqrt(ans)
pause

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

syms x y
(x - y)*(x - y)^2
expand(ans)
factor(ans)
pause

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

simplify(sin(x)*cos(y)+cos(x)*sin(y))
simple(sin(x)*cos(y)+cos(x)*sin(y))

cos(pi/2)
cos(sym('pi/2'))
sym('1/2') + sym('1/3')
vpa('sqrt(2)', 50)
pause

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

whos
pause

u = pi; v = 'pi'; w = sym('pi');
whos
sin('pi')
pause

%The next line produces the indicated error message;
%it is commented out so as to not interrupt the flow
%of the M-file
%
%3u^2

x = 7
x^2 - 2*x*y + y
clear x; syms x y
z = x^2 - 2*x*y + y
5*y*z
pause

solve('x^2 - 2*x - 4 = 0')
double(ans)
solve('2*x - log(y) = 1','y')
pause

[x, y] = solve('x^2 - y = 2', 'y - 2*x = 5')
x(1)
y(1)
pause

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

solve('sin(x) = 2 - x')
solve('exp(-x) = sin(x)')
fzero(inline('exp(-x) - sin(x)'), 0.5)
pause

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

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

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

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

log(exp(3))
sin(2*pi/3)
sin(sym('2*pi/3'))
pause

f = inline('x^2 + x + 1', 'x')
f(4)
f1 = inline(vectorize('x^2 + x + 1'), 'x')
f1(1:5)
pause

g = inline('u^2 + v^2', 'u', 'v')
ezplot('x^2 + x + 1', [-2 2])
pause
print -deps doingmat2.eps

syms x; ezplot(x^2 + x + 1, [-2 2])
pause

title 'A Parabola'
xlabel 'x-axis'; ylabel 'y-axis';
axis([-2 2 1 4])
pause

X = [1 2 3];
Y = [4 6 5];
plot(X, Y)
pause
print -deps doingmat3.eps

X=-2:0.1:2;
plot(X, X.^2 + X + 1)
pause
print -deps doingmat4.eps

plot(X, f1(X))
pause

ezplot('exp(-x)', [0 10])
hold on
ezplot('sin(x)', [0 10])
hold off
title 'exp(-x) and sin(x)'
pause
print -deps doingmat1.eps

X = 0:0.1:10;
plot(X, exp(-X), X, sin(X))
pause

echo off
diary off
