This commit is contained in:
lda committed 2025-06-05 02:28:28 +00:00
1 parent 366c631d27
commit 3d83509de5
32 files changed
+1258

No files matched your search

+206
View File
@@ -0,0 +1,206 @@
%% Labwork 2 Solutions
%%% III. Polynomials
%% Exercise 1: Arithmetic Operations
% a) 12^2 - 13^2
a = 12^2 - 13^2
% b) 24 * 5 / 1.5
b = 24 * 5 / 1.5
% c) 7! (7 factorial)
c = factorial(7)
% d) 4^2 + 3^3 - 124
d = 4^2 + 3^3 - 124
% e) 6/5 * (1/2 * 1/4 - 1)
e = (6/5) * ((1/2) * (1/4) - 1)
%% Exercise 2: fzero
% Given f(x) = x^2 - 9
% a) Find the positive root using fzero
f = @(x) x^2 - 9; % Define the function
positive_root = fzero(f, 2) % Initial guess of 2 (can be any positive number)
% b) Initial guess of zero
root_zero_guess = fzero(f, 0) % It might give a warning, and the result can vary slightly due to how fzero works
% c) Display all iterations
options = optimset('Display', 'iter');
[root_iterations, fval_iterations, exitflag_iterations, output_iterations] = fzero(f, 2, options)
% d) Set tolerance: tolx = 10^-3
options_tol = optimset('TolX', 10^-3);
root_tolerance = fzero(f, 2, options_tol)
% e) Verify with roots
coefficients = [1 0 -9]; % Coefficients of the quadratic equation
all_roots = roots(coefficients)
%% Exercise 3: plot
% Given v(t) = sqrt(gm/cd) * tanh(sqrt(g*cd/m) * t)
% g = 9.81, m = 68.1, cd = 0.25
g = 9.81;
m = 68.1;
cd = 0.25;
t = 0:2:20; % Time vector from 0 to 20 seconds in steps of 2
v = sqrt((g*m)/cd) .* tanh(sqrt((g*cd)/m) .* t); % Calculate velocity
% a) Plot (t, v)
figure; % Create a new figure
plot(t, v, 'b-o'); % Plot v against t ('b-o' for blue line with circles)
% b) Customize the graph
title('Plot of v versus t');
xlabel('Values of t');
ylabel('Values of v');
grid on;
%%% IV. System of Non-linear Equations
%% Exercise 1: Newton-Raphson
% a) Use ezplot to derive good guesses
% (This part is visual, so I'll show the code to generate the plot for you to estimate)
figure;
ezplot(@(x1, x2) x1^2 + x1*x2 - 10, [-6 6]); % Plot the first equation
hold on;
ezplot(@(x1, x2) x2 + 3*x1*x2^2 - 57, [-6 6]); % Plot the second equation
hold off;
title('Plot of equations for initial guesses');
xlabel('x1');
ylabel('x2');
grid on;
% Inspect the plot to get approximate intersection points (guesses).
% b) Explain the given code
% (Explanation embedded as comments in the code below)
clc % Clear command window
clear all % Clear variables
close all % Close all figures
% Define the initial guess as a column vector
x = [1; 1];
N = 50; % Maximum number of iterations
for k = 1:N % Loop for iterations
% Define the system of equations
F = [x(1)^2 + x(1)*x(2) - 10;
x(2) + 3*x(1)*x(2)^2 - 57];
% Calculate the Jacobian matrix
A = [2*x(1) + x(2), x(1);
3*x(2)^2, 1 + 6*x(1)*x(2)];
% Solve for the change in x (dx)
dx = A \ F;
% Update x
x = x - dx;
% Increment the iteration counter (though it's redundant here, as the loop does it)
%k = k + 1;
end
k % Display the final iteration number
x' % Display the solution as a row vector
F % Display the function values at the solution (should be close to zero)
% c) Create root2d.m and use fsolve
%
% Create a new MATLAB function file named root2d.m with the following content:
% ------------------------------------------------------------------
% function F = root2d(x)
% F(1) = x(1)^2 + x(1)*x(2) - 10;
% F(2) = x(2) + 3*x(1)*x(2)^2 - 57;
% end
% ------------------------------------------------------------------
%
% Then, in a separate script:
% Call fsolve with the function and an initial guess
f_system = @root2d;
x0 = [0; 0]; % Initial guess
[solution_fsolve, fval_fsolve] = fsolve(f_system, x0)
% Explanation:
% - root2d.m defines the system of equations as a function that takes a vector x
% and returns a vector F representing the equations.
% - fsolve finds the solution to the system of nonlinear equations. It takes the
% function handle (@root2d) and an initial guess (x0) as input.
% d) Use newtmult.m
% (Assuming newtmult.m is provided as in the lab, place it in the same directory)
% Define the function (same as in root2d.m)
func = @(x) [x(1)^2 + x(1)*x(2) - 10; x(2) + 3*x(1)*x(2)^2 - 57];
x0 = [0; 0]; % Initial guess
es = 0.0001; % Desired relative error
maxit = 50; % Maximum iterations
[x_newtmult, f_newtmult, ea_newtmult, iter_newtmult] = newtmult(func, x0, es, maxit)
% Explanation of newtmult.m (from the lab):
% - It implements the Newton-Raphson method for systems of nonlinear equations.
% - Inputs: function handle (func), initial guess (x0), desired relative error (es),
% maximum iterations (maxit), and any additional parameters for the function.
% - Outputs: solution (x), function values at the solution (f), approximate relative error (ea),
% and number of iterations (iter).
% - The code iteratively calculates the Jacobian, solves a linear system, and updates the
% solution until convergence or the maximum number of iterations is reached.
%%% V. Common
%% Exercise 1: Calculate the value of x
% a) log(x) = 770 (Assuming base 10 logarithm)
x_a = 10^770
% b) e^x = 15
x_b = log(15)
% c) sin(x) = 0.12 (in radians)
x_c = asin(0.12)
% d) sin(x + pi/5) = 0.25 (in radians)
x_d = asin(0.25) - pi/5
% e) tan(x + 2*pi/3) = 3 (in radians)
x_e = atan(3) - 2*pi/3
%% Exercise 2: Create a vector x
% Create the row vector
x_vector = [2 3 1 5 11]
% a) Find length and size
length_x = length(x_vector)
size_x = size(x_vector)
% - length() returns the number of elements in the longest dimension.
% - size() returns a row vector with the number of rows and columns.
% b) Find the transpose
x_transpose = x_vector'
% c) Calculate the product of x and its transpose
product_x_xt = x_vector * x_transpose
%% Exercise 3: Build the matrix B
% Build the matrix B
B = [1:7; 9:-2:-3; 4.^(1:7)]
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.