added source file because of gitea webview not supporting .mlx
added with save as
This commit is contained in:
1 parent
bb5767833b
commit
6e7b1062c1
5 files changed
+1549
No files matched your search
+252
@@ -0,0 +1,252 @@
|
||||
%% I. Matlab Introduction
|
||||
%% 1. Vector
|
||||
|
||||
% Exercise 1
|
||||
x = [1, 2, 3, 4, 5];
|
||||
result1 = x(3) + x(2)
|
||||
|
||||
% Exercise 2
|
||||
x_col = [1; 2; 3; 4; 5];
|
||||
x_transposed = x_col'
|
||||
|
||||
% Exercise 3
|
||||
a_sum = sum(1:100)
|
||||
|
||||
% Exercise 4
|
||||
syms x y
|
||||
f = x + y;
|
||||
f_val = subs(f, [x, y], [1, 2])
|
||||
|
||||
% Exercise 5
|
||||
% a) sum from 1 to 10
|
||||
syms n
|
||||
s1 = symsum(n, n, 1, 10)
|
||||
|
||||
% b) sum of n^2 from 1 to 5
|
||||
s2 = symsum(n^2, n, 1, 5)
|
||||
%% 2. Matrix
|
||||
|
||||
% Exercise 1a
|
||||
% Define element by element
|
||||
A1 = [0 0 0; 0 0 0; 0 0 0];
|
||||
|
||||
% Define using zeros
|
||||
A2 = zeros(3,3);
|
||||
|
||||
% Exercise 1b
|
||||
A = zeros(3);
|
||||
A = A + diag([1 1 1])
|
||||
|
||||
% Exercise 1c
|
||||
B = A'
|
||||
|
||||
% Exercise 1d
|
||||
add = A + B
|
||||
sub = A - B
|
||||
mul_matrix = A * B
|
||||
mul_elem = A .* B
|
||||
% Remark: A*B is matrix multiplication, A.*B is element-wise
|
||||
|
||||
% Exercise 1e
|
||||
A2x2 = A;
|
||||
A2x2(3,:) = [];
|
||||
A2x2(:,3) = [];
|
||||
A2x2
|
||||
|
||||
B2x2 = B;
|
||||
B2x2(1,:) = [];
|
||||
B2x2(:,1) = [];
|
||||
B2x2
|
||||
|
||||
% Exercise 1f
|
||||
A(1:3)
|
||||
A(1:3,1)
|
||||
A(:,1)
|
||||
%% 3. Function
|
||||
|
||||
% Exercise 1
|
||||
function m = mean_vector(v)
|
||||
m = sum(v)/length(v);
|
||||
end
|
||||
|
||||
% Application
|
||||
x = 1:3:100;
|
||||
m_val = mean_vector(x)
|
||||
|
||||
% Exercise 2
|
||||
function roots = quadratic_solver(a, b, c)
|
||||
D = b^2 - 4*a*c;
|
||||
roots = [(-b + sqrt(D))/(2*a), (-b - sqrt(D))/(2*a)];
|
||||
end
|
||||
|
||||
% Application
|
||||
roots_val = quadratic_solver(1, 2, -3)
|
||||
%% 4. If else structure
|
||||
|
||||
% Exercise 1
|
||||
n = 5;
|
||||
if n > 1
|
||||
m = n + 1;
|
||||
else
|
||||
m = n - 1;
|
||||
end
|
||||
m
|
||||
|
||||
n = -5;
|
||||
if n > 1
|
||||
m = n + 1;
|
||||
else
|
||||
m = n - 1;
|
||||
end
|
||||
m
|
||||
|
||||
% Exercise 2
|
||||
x = 5;
|
||||
if x > 0 && x < 10
|
||||
y = 4*x;
|
||||
elseif x > 10 && x < 40
|
||||
y = 10*x;
|
||||
elseif x > 40 || x < 0
|
||||
y = x;
|
||||
end
|
||||
y
|
||||
|
||||
x = -5;
|
||||
if x > 0 && x < 10
|
||||
y = 4*x;
|
||||
elseif x > 10 && x < 40
|
||||
y = 10*x;
|
||||
elseif x > 40 || x < 0
|
||||
y = x;
|
||||
end
|
||||
y
|
||||
|
||||
x = 20;
|
||||
if x > 0 && x < 10
|
||||
y = 4*x;
|
||||
elseif x > 10 && x < 40
|
||||
y = 10*x;
|
||||
elseif x > 40 || x < 0
|
||||
y = x;
|
||||
end
|
||||
y
|
||||
|
||||
% Exercise 3: Electricity bill
|
||||
kWh = 350;
|
||||
price = 0; % cant even initialize bru
|
||||
if kWh > 400
|
||||
price = (kWh-400)*2.587 + 100*2.503 + 100*2.242 + 100*1.786 + 50*1.533 + 50*1.484;
|
||||
elseif kWh > 300
|
||||
price = (kWh-300)*2.503 + 100*2.242 + 100*1.786 + 50*1.533 + 50*1.484;
|
||||
elseif kWh > 200
|
||||
price = (kWh-200)*2.242 + 100*1.786 + 50*1.533 + 50*1.484;
|
||||
elseif kWh > 100
|
||||
price = (kWh-100)*1.786 + 50*1.533 + 50*1.484;
|
||||
elseif kWh > 50
|
||||
price = (kWh-50)*1.533 + 50*1.484;
|
||||
else
|
||||
price = kWh * 1.484;
|
||||
end
|
||||
price
|
||||
%% 5. For loop
|
||||
|
||||
% Exercise 1a: sum of squares
|
||||
sum_sq = 0;
|
||||
for i = 1:1000
|
||||
sum_sq = sum_sq + i^2;
|
||||
end
|
||||
sum_sq
|
||||
|
||||
% Exercise 1b: 20!
|
||||
fact = 1;
|
||||
for i = 1:20
|
||||
fact = fact * i;
|
||||
end
|
||||
fact
|
||||
|
||||
% Exercise 1c: Series
|
||||
sum_alt = 0;
|
||||
for i = 1:2:1003
|
||||
sum_alt = sum_alt + ((-1)^((i-1)/2)) * (1/i);
|
||||
end
|
||||
sum_alt
|
||||
|
||||
% Exercise 1d: π approximation
|
||||
sum_pi = 0;
|
||||
for k = 1:1000
|
||||
sum_pi = sum_pi + 1/((2*k-1)^2);
|
||||
end
|
||||
pi_squared_over_8 = sum_pi
|
||||
approx_pi = sqrt(8 * sum_pi)
|
||||
|
||||
% Exercise 2a: Combination function
|
||||
function c = combination(n, k)
|
||||
c = factorial(n)/(factorial(k)*factorial(n-k));
|
||||
end
|
||||
|
||||
comb_val = combination(5, 3)
|
||||
|
||||
% Exercise 2b: Permutation function
|
||||
function p = permutation(n, k)
|
||||
p = factorial(n)/factorial(n-k);
|
||||
end
|
||||
|
||||
perm_val = permutation(5, 3)
|
||||
%% 6. While loop
|
||||
|
||||
% Exercise 1a: Count numbers from 1 to N
|
||||
N = 5;
|
||||
i = 1;
|
||||
while i <= N
|
||||
disp(i)
|
||||
i = i + 1;
|
||||
end
|
||||
|
||||
% Exercise 1b: Count odd numbers from 1 to N
|
||||
i = 1;
|
||||
while i <= N
|
||||
if mod(i,2) == 1
|
||||
disp(i)
|
||||
end
|
||||
i = i + 1;
|
||||
end
|
||||
|
||||
% Exercise 2: Sum sqrt(2i-1) until > 20
|
||||
i = 1;
|
||||
S = 0;
|
||||
while S <= 20
|
||||
S = S + sqrt(2*i - 1);
|
||||
i = i + 1;
|
||||
end
|
||||
S
|
||||
i
|
||||
|
||||
% Exercise 3: Doubling investment
|
||||
investment = 1;
|
||||
years = 0;
|
||||
while investment < 2
|
||||
investment = investment * 1.1;
|
||||
years = years + 1;
|
||||
end
|
||||
years
|
||||
%% 7. Graphics
|
||||
|
||||
% Exercise 1a: Linear plot
|
||||
x = linspace(-5,5,100);
|
||||
y1 = x + 1;
|
||||
plot(x, y1)
|
||||
|
||||
% Exercise 1b: Line style and color
|
||||
plot(x, y1, '--r')
|
||||
|
||||
% Exercise 1c: Plot second function
|
||||
hold on
|
||||
y2 = -x + 2;
|
||||
plot(x, y2, ':b')
|
||||
hold off
|
||||
|
||||
% Exercise 1d: Add title and labels
|
||||
title('Linear Functions')
|
||||
xlabel('x')
|
||||
ylabel('y')
|
||||
legend('y = x+1', 'y = -x+2')
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
%% I. MATLAB FUNCTION
|
||||
%% 1. Solve / fzero
|
||||
|
||||
% (a) Exact solution: 67x + 32 = 0
|
||||
syms x
|
||||
sol_exact = solve(67*x + 32 == 0, x)
|
||||
|
||||
% (b) Numerical solution to 15 places
|
||||
format long
|
||||
f = @(x) 67*x + 32;
|
||||
sol_numeric = fzero(f, -1)
|
||||
|
||||
% (c) x^3 + p*x + q = 0
|
||||
syms x p q
|
||||
sol_cubic = solve(x^3 + p*x + q == 0, x)
|
||||
|
||||
% (d) e^x = 8x - 4
|
||||
syms x
|
||||
eqn = exp(x) == 8*x - 4;
|
||||
% Plotting to identify real solutions
|
||||
fplot(@(x) exp(x), [-2, 2]); hold on;
|
||||
fplot(@(x) 8*x - 4, [-2, 2]);
|
||||
legend('e^x','8x-4');
|
||||
grid on;
|
||||
title('Graph of e^x and 8x-4 to visualize intersection points');
|
||||
% Solving numerically
|
||||
f = @(x) exp(x) - (8*x - 4);
|
||||
sol1 = fzero(f, 0) % Guess from plot
|
||||
%% 2. Plot
|
||||
|
||||
% (a) y = x^3 - x for x in [-4, 4]
|
||||
x = linspace(-4, 4, 1000);
|
||||
y = x.^3 - x;
|
||||
figure
|
||||
plot(x, y); grid on; title('y = x^3 - x');
|
||||
|
||||
% (b) y = tan(x/2) for x in [-pi, pi], clip y in [-10, 10]
|
||||
x = linspace(-pi, pi, 1000);
|
||||
y = tan(x/2);
|
||||
figure
|
||||
plot(x, y);
|
||||
axis([-pi pi -10 10]);
|
||||
title('y = tan(x/2)');
|
||||
|
||||
% (c) y1 = e^(-x/2), y2 = x^4 - x^2 for x in [-1.5, 1.5]
|
||||
x = linspace(-1.5, 1.5, 1000);
|
||||
y1 = exp(-x/2);
|
||||
y2 = x.^4 - x.^2;
|
||||
figure
|
||||
plot(x, y1, 'b', x, y2, 'r--');
|
||||
legend('e^{-x/2}', 'x^4 - x^2');
|
||||
title('Overlay of two functions');
|
||||
%% II. MATLAB NUMERICAL METHODS
|
||||
%% 1. Bisection Method
|
||||
|
||||
% Function file already given above as bisection.m
|
||||
|
||||
% Exercise 1: sqrt(2), f(x) = x^2 - 2
|
||||
f = @(x) x^2 - 2;
|
||||
[root1, fx1, ea1, iter1] = bisection(f, 1, 2, 0.05)
|
||||
|
||||
% Exercise 2: ln(x^2) = 0.7 --> f(x) = log(x^2) - 0.7
|
||||
f = @(x) log(x^2) - 0.7;
|
||||
[root2, fx2, ea2, iter2] = bisection(f, 0.5, 2, 100, 3)
|
||||
|
||||
% Exercise 3: x^2 + 2x - 3 = 0
|
||||
f = @(x) x^2 + 2*x - 3;
|
||||
[root3, fx3, ea3, iter3] = bisection(f, 0, 2)
|
||||
%% 2. Newton-Raphson Method
|
||||
|
||||
% f.m file
|
||||
% function y = f(x)
|
||||
% y = x^3 + x - 3;
|
||||
|
||||
% df.m file
|
||||
% function y = df(x)
|
||||
% y = 3*x^2 + 1;
|
||||
|
||||
% Exercise 1: Solve x^3 + x - 3 = 0
|
||||
f = @(x) x^3 + x - 3;
|
||||
df = @(x) 3*x^2 + 1;
|
||||
x0 = 1; tol = 1e-8; max_iter = 20;
|
||||
x = x0;
|
||||
for i = 1:max_iter
|
||||
x_new = x - f(x)/df(x);
|
||||
err = abs((x_new - x)/x_new);
|
||||
x = x_new;
|
||||
fprintf('Iter %d: x = %.15f, f(x) = %.15f\n', i, x, f(x));
|
||||
if err < tol
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
% Exercise 2: f(x) = x^10 - 1, x0 = 0.5
|
||||
f = @(x) x^10 - 1;
|
||||
df = @(x) 10*x^9;
|
||||
x = 0.5;
|
||||
X_history = x;
|
||||
for i = 1:20
|
||||
x_new = x - f(x)/df(x);
|
||||
X_history(end+1) = x_new;
|
||||
if abs((x_new - x)/x_new) < 1e-8
|
||||
break
|
||||
end
|
||||
x = x_new;
|
||||
end
|
||||
fprintf('Final approx root: %.15f\n', x);
|
||||
|
||||
% Plot convergence
|
||||
figure;
|
||||
plot(0:length(X_history)-1, X_history, '-o');
|
||||
xlabel('Iteration');
|
||||
ylabel('x');
|
||||
title('Newton-Raphson convergence for x^{10} - 1');
|
||||
|
||||
% Exercise 3: Solve x^2 + 2x - 3 = 0
|
||||
f = @(x) x^2 + 2*x - 3;
|
||||
df = @(x) 2*x + 2;
|
||||
x = 1;
|
||||
for i = 1:20
|
||||
x_new = x - f(x)/df(x);
|
||||
fprintf('Iter %d: x = %.10f, f(x) = %.10f\n', i, x_new, f(x_new));
|
||||
if abs((x_new - x)/x_new) < 1e-8
|
||||
break
|
||||
end
|
||||
x = x_new;
|
||||
end
|
||||
Reference in new issue
Block a user