%% beyondbasics.m, chapter M-file for "Beyond the Basics" chapter
graphicsdefaults

clear
close all
%% 4.1 Suppressing Output
syms x 
y = x + 7

z = x + 7;
z

x = 7; disp(x)

disp(solve('x + tan(y) = 5', 'y'))
%% 4.2 Data Classes
a = 10; b = 5; factor(a^2 - b^2)
b = 'b';
factor(b^2)

help factor
help sym/factor
%% 4.2.1 String Manipulation

eqn =['left hand side of equation = ', ...
'right hand side of equation']

disp(['The value of 10! is ', num2str(factorial(10)), '.'])
%% 4.2.2 Symbolic and Floating Point Numbers
a = 1
b = a/sym(2)
c = sqrt(3)
sym(c)
sym('1 + sqrt(3)')
%% 4.3 functions and Expressions
f = 'x^3 - 1';
f(7)
f(5)
%Typing f(-1) does yield the predicted error message 

h = @(t) t^3;
int(sym('h(t)'),'t')
syms t
int(h(t),'t')

%% 4.3.1 Substitution

%% 4.4 More about M-Files
%% 4.4.1 Variables in Script M-Files

%Typing scriptex2 produces an error message:
%Undefined function or variable 'u'.
scriptex1
scriptex2

%% 4.4.2 Variables in Function M-Files
x = 4; z = 'junk';syms a x y
sq(3)

%% 4.4.3 Structure of Function M-Files
help sq

polarcoordinates(3, 4)
[r, theta] = polarcoordinates(3, 4)
r = polarcoordinates(3, 4)
theta = polarcoordinates(3, 4)
%% 4.5 Complex Arithmetic
solve('x^2 + 2*x + 2 = 0')
log(-1)
(2 + 3*i)*(4 - i)
%% 4.6 More on Matrices

%% 4.6.1 Solving Linear systems
A = [3 -2 0; 2 -2 0; 0 1 1];
eig(A)
[U, R] = eig(A)
[U, R] = eig(sym(A))

%% 4.7 Doing Calculus with MATLAB
%% 4.7.1 Differentiation
syms x; diff(x^3)
f=@(x) x^3; diff(f(x))
diff(f(x), 2)
syms y z
diff(x^2*y, y)
diff(diff(sin(x*y/z), x), y)
dsolve('x*Dy + 1 = y', 'x')
syms y(x); dsolve(x*diff(y) + 1 == y, x)

%% 4.7.2 Integration
syms x; int(x^2)
int(asin(x), 0, 1)
hardintegral = int(log(1+x^2)*exp(-x^2), 0, 1)
format long, double(hardintegral)
integral(@(x) log(1+x.^2).*exp(-x.^2), 0, 1)
integral(@(x) abs(x), -1, 2)
integral(@(x) abs(x), -1, 2, 'Waypoints', 0)
syms y; int(int(x^2 + y^2, y, 0, sin(x)), 0, pi)

%% 4.7.3 Limits
syms x; limit(sin(x)/x, x, 0)
limit(abs(x)/x, x, 0, 'left')
limit((x^4 + x^2 - 3)/(3*x^4 - log(x)), x, Inf)

%% 4.7.4 Sums and Products
X = 1:7;
sum(X)
prod(X)
symsum(1/n^2, 1, Inf)
syms a k; symsum(a^k, 0, Inf)

%% 4.7.5 Taylor Series
syms x; taylor(sin(x), x, 10)
taylor(sqrt(x), x, 4, 'Order', 3)
taylor(exp(1/x^2), x, Inf)

%% 4.8 Default Variables
syms t; diff(sin(t^2))
solve('x + y = 3')
solve('x + y = 3', 'y')
syms w z; diff(w*z)

evalin(symengine, 'nextprime(10^20)')