Skip to content
Snippets Groups Projects
Commit 8afd1117 authored by Baresi's avatar Baresi
Browse files

source codes for MATLAB crash course

parents
No related branches found
No related tags found
No related merge requests found
Showing with 349 additions and 0 deletions
*.png
*.asv
*~
clear all; close all; clc; format longG;
base = 5; % m
height = 10; % m
a = triArea(base, height);
fprintf('Area of triangle is %f m^2\n', a);
\ No newline at end of file
function a = triArea(b, h)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a = triArea(b,h)
%
% Calculate the area of a triangle
%
% INPUT:
% - b base of triangle
% - h height of triangle
%
% OUTPUT:
% - a area of triangle
%
% Author: N. Baresi
% Date: Sep 2023
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
base = b;
height = h;
a = base*height/2;
end
\ No newline at end of file
clear all; close all; clc; format longG;
x = linspace(0, 2*pi, 100);
y = sin(x);
figure()
plot(x, y);
\ No newline at end of file
clear all; close all; clc; format longG;
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
figure(), hold on, grid on;
plot(x, y1, 'b-o', 'LineWidth', 2);
plot(x, y2, 'r-o', 'LineWidth', 2);
xlabel('x [-]')
ylabel('y [-]')
title('Harmonic Functions')
legend('Sine','Cosine')
clear all; close all; clc; format longG;
a = 10;
for ii = 1:10
fprintf('Value of a is %d\n', a);
a = a + 5;
end
fprintf('For loop has finished\n')
\ No newline at end of file
clear all; close all; clc; format longG;
b = 10;
for ii = 1:10
fprintf('Value of b is %d\n', b);
b = b + 5;
if(b >= 50)
break;
end
end
fprintf('For loop has finished\n');
\ No newline at end of file
clear all; close all; clc; format longG;
a = 10;
%% IF Statement
if(a > 5)
fprintf('a is greater than 5!\n');
end
%% IF-ELSE statement
if(a < 5)
fprintf('a is less than 5!\n');
else
fprintf('a is greater than 5!\n');
end
%% IF-ELSEIF-...-ELSE Statement
if(a < 5)
fprintf('a is less than 5!\n');
elseif(a >=5 && a < 10)
fprintf('a is greater or equal than 5, but less then 10\n');
else
fprintf('a is greater or equal than 10!\n');
end
clear all; close all; clc; format longG;
% create a 4x3 matrix with normally distributed elements
A = [1.3, 1.2, 1.3;
3.1, 2.4, 3.1;
4.1, 4.2, 4.3;
7.1, 8.4, 9.1];
% loop over rows
for ii = 1:4
% loop over columns
for jj = 1:3
fprintf('A(%d,%d) = %f\n',ii, jj, A(ii,jj));
end
end
clear all; close all; clc; format longG;
a = 10;
while(a < 50)
fprintf('Value of a is %d\n', a);
a = a + 5;
end
fprintf('while loop has finished\n');
\ No newline at end of file
function dXdt = pendulum_ode(t, X, l, g)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% dXdt = pendulum_ode(t, X, l, g)
%
% System of first-order ODEs for the pendulum problem
%
% INPUTS:
% - t time
% - X state vetor [angular position, angular velocity]
% - l pendulum length [m]
% - g gravitational acceleration [m/s^2]
%
% OUTPUTS:
% - dXdt time derivative of state vector [angular vel., angular acc.]
%
% Author: N. Baresi
% Date: Sep 2023
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Retrieve inputs
theta = X(1);
omega = X(2);
% initialize output
dXdt = zeros(2,1);
% Differential equations
dXdt(1) = omega;
dXdt(2) = -g/l*sin(theta);
end
File added
clear all; close all; clc; format longG;
set(0, 'DefaultTextInterpreter', 'latex');
set(0, 'DefaultAxesFontSize', 20);
set(0, 'DefaultLineLineWidth', 2);
l = 1.0; % pendulum length (m)
g = 9.81; % gravitational acceleration (m/s^2)
% Set initial conditions
theta0 = pi/8;
omega0 = 0.1;
X0 = [theta0; omega0];
% Set time vector
tstart = 0;
tend = 10*2*pi*sqrt(l/g);
tspan = [tstart, tend];
% Ode-options
ode_opt = odeset('RelTol', 3e-12, 'AbsTol', 1e-14);
% Call to ODE45 (replace with ODE113 if you prefer)
[tout, Xout] = ode45(@pendulum_ode, tspan, X0, ode_opt, l, g);
% Store output in new variables
time = tout;
thetat = Xout(:, 1); % Recall: X = [theta, omega]
omegat = Xout(:, 2);
%% Plot outputs
figure()
yyaxis left;
plot(time, thetat);
xlabel('Time (s)');
ylabel('$\theta$ (rad)');
xlim([0, time(end)]);
ylim([1.2*min(thetat), 1.2*max(thetat)]);
box on; grid on;
yyaxis right;
plot(time, omegat);
ylabel('$\omega$ (rad/s)');
ylim([1.2*min(omegat), 1.2*max(omegat)]);
box on; grid on;
savefig('pendulum_plots.fig');
print('pendulum_plots.png','-dpng');
%% Computing the Energy and Speed Comparison
disp('Energy Calculation: For Loop');
tic
E_vec = zeros(length(thetat), 1);
for ii = 1:length(thetat)
E_vec(ii) = 0.5*omegat(ii)^2 - (g/l)*cos(thetat(ii));
end
toc
disp('Energy Calculation: Vectorised code');
tic
E_vec = 0.5*(omegat.^2) - (g/l)*cos(thetat);
toc
function dXdt = springmass_ode()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% dXdt = springmass_ode()
%
% INPUTS
%
% OUTPUTS
% .dXdt
%
% Author:
% Date:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Retrieve initial conditions
x =
v =
% initialize output
dXdt = zeros( , );
% Differential equations
dXdt(1) =
dXdt(2) =
end
clear all; close all; clc; format longG;
set(0, 'DefaultTextInterpreter', 'latex');
set(0, 'DefaultAxesFontSize', 20);
set(0, 'DefaultLineLineWidth', 2);
% Define problem parameters
k =
m =
% Define initial conditions
x =
v =
X =
% Define time vector
t0 =
tf =
tspan
% Ode-options
ode_opt = odeset('RelTol', ,'AbsTol', );
% Call ode113
[tout, Xout] = ode113();
% store output in new variables
time = tout;
x =
v =
% Plot output
figure()
savefig('springmass_plots.fig');
print('springmass_plots.png', '-dpng');
% Calculate Energy
clear all; close all; clc; format longG;
x0 = 20;
tol = 1e-10;
[x, no_of_iter] = newton_method_for_loop(x0, tol);
fprintf('The final value of x after %d iterations is %f\n', no_of_iter, x);
\ No newline at end of file
function [xf, no_of_iterations] = newton_method_for_loop(x0, tol)
% retrieve initial guess
xn = x0;
for ii = 1:10
fxn = sin(xn) + xn*cos(xn); % evaluate function in xn
fpxn = cos(xn) + cos(xn) - xn*sin(xn); % evaluate first derivative in xn
% Print output
fprintf('xn: %f, |f(xn)|: %e\n', xn, abs(fxn));
% check for convergence using IF statement
if(abs(fxn) < tol)
break;
else
% Newton's method
dxn = -fxn/fpxn; % delta x_n
xn = xn + dxn; % update value of xn
end
end
% output variables
xf = xn;
no_of_iterations = ii;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment