소스 검색

Add files from class

Pjotir 1 개월 전
부모
커밋
63d8d87e48

BIN
Homework2/octave-workspace


+ 14 - 0
classwork/ODE_4thOrder.m

@@ -0,0 +1,14 @@
+clear; clc; close all;
+
+ode_system = @(t, Y) [
+    Y(2);
+    Y(3);
+    Y(4);
+    cos(2*t) - Y(1) - (1-Y(1)^2)*Y(2) - (1+t)*Y(3) - 2*Y(4)
+]
+
+t_span = [0 10];
+initial_conditions = [1 0 0 0];
+[t, Y] = ode45(ode_system, t_span, initial_conditions);
+y_sol = Y(:, 1);
+plot(t, y_sol);

+ 40 - 0
classwork/higher_order_odes.m

@@ -0,0 +1,40 @@
+% 1. Clear the workspace and figures
+clear; clc; close all;
+
+% 2. Define the differential equation system
+% The solver expects a function with inputs (t, Y) where Y is a vector:
+% Y(1) represents y, and Y(2) represents dy/dt.
+ode_system = @(t, Y) [
+    Y(2);                           % dY1/dt = Y2
+    Y(3);                           % dY2/dt = Y3
+    -3*Y(3)-(1-Y(1)^2)*Y(2)+Y(1)    % dY3/dt =
+];
+
+% 3. Set the time span for simulation [start_time, end_time]
+t_span = [0, 5];
+
+% 4. Define initial conditions: [initial_y, initial_dydt]
+% For example: y(0) = 0.5 and dy/dt(0) = 0.0
+initial_conditions = [0.5; 0.0; 0];
+
+% 5. Solve the system numerically using ode45
+[t, Y] = ode45(ode_system, t_span, initial_conditions);
+
+% 6. Extract the individual states from the solution matrix
+y_sol      = Y(:, 1);  % First column is y(t)
+dydt_sol   = Y(:, 2);  % Second column is dy/dt(t)
+d2ydt2_sol = Y(:, 3);  % Third column is d2y/dt2(t)
+
+% 7. Plot the results
+figure;
+plot(t, y_sol, '-b', 'LineWidth', 2);
+hold on;
+plot(t, dydt_sol, '--r', 'LineWidth', 1.5);
+grid on;
+
+% Labeling the graph
+title('Numerical Solution of Higher-Order ODE');
+xlabel('Time (t)');
+ylabel('State Values');
+legend('y(t) (Position)', 'dy/dt (Velocity)', 'Location', 'best');
+

+ 31 - 0
classwork/pendulum_animation_template.m

@@ -0,0 +1,31 @@
+clear; clc; close all
+
+
+%-------------------------------------------------
+% Pendulum's base position
+x0 = 0; y0 = 0;
+
+% Animation loop
+figure;
+for i = 1:length(t)
+
+    % Update pendulum position
+
+    
+    % string
+
+    hold on
+    % ball
+
+
+    hold off
+    axis equal;
+    xlim([-L, L]);  % Set x-axis limits
+    ylim([-L, 0.5]);  % Set y-axis limits
+    xlabel('x (m)');
+    ylabel('y (m)');
+    title(sprintf('Pendulum Animation (Angle = %.0f deg)', rad2deg(theta0)),'FontSize',14);
+    pause(0.1);
+
+end
+

+ 51 - 0
classwork/pendulum_damped.m

@@ -0,0 +1,51 @@
+clc; clear all; close all;
+
+c = 2; m = 1; g = 9.81; L = 2; initial_angle = deg2rad(30);
+
+ode_system = @(t, theta) [
+    theta(2);
+    ((-m*g*L*sin(theta(1))) - (c*theta(2))) / (m*L^2)
+];
+
+t_span = [0, 10]
+initial_conditions = [initial_angle, 0];
+[t, theta] = ode45(ode_system, t_span, initial_conditions);
+theta_sol = theta(:, 1);
+dthetadt_sol = theta(:, 2);
+
+figure;
+plot(t, theta_sol);
+
+%-------------------------------------------------
+% Pendulum's base position
+x0 = 0; y0 = 0;
+
+% Animation loop
+figure;
+axis equal;
+xlim([-L, L]);  % Set x-axis limits
+ylim([-L, 0.5]);  % Set y-axis limits
+xlabel('x (m)');
+ylabel('y (m)');
+title(sprintf('Pendulum Animation (Angle = %.0f deg)', rad2deg(initial_angle)),'FontSize',14);
+
+for i = 1:length(t)
+
+    % Update pendulum position
+    x = x0 + L*sin(theta_sol(i));
+    y = y0 - L*cos(theta_sol(i));
+
+    hold on;
+
+    % string
+    plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
+    % ball
+    plot(x, y, 'ro', 'MarkerSize', 20);
+
+
+    drawnow;
+    pause(0.1);
+    hold off;
+    cla;
+end
+

+ 55 - 0
classwork/pendulum_ode.m

@@ -0,0 +1,55 @@
+clc; clear all; close all;
+
+% Constants
+g = 9.81;
+L = 2;
+initial_angle = deg2rad(60);
+initial_v = 0;
+initial_conditions = [initial_angle; initial_v];
+t_span = [0 3];
+
+ode_system = @(t, theta) [
+    theta(2);
+    -(g/L)*sin(theta(1))
+];
+
+[t, theta] = ode45(ode_system, t_span, initial_conditions);
+theta_sol = theta(:, 1);
+dthetadt_sol = theta(:, 2);
+
+figure;
+plot(t, theta_sol);
+
+%-------------------------------------------------
+% Pendulum's base position
+x0 = 0; y0 = 0;
+
+% Animation loop
+figure;
+axis equal;
+xlim([-L, L]);  % Set x-axis limits
+ylim([-L, 0.5]);  % Set y-axis limits
+xlabel('x (m)');
+ylabel('y (m)');
+title(sprintf('Pendulum Animation (Angle = %.0f deg)', rad2deg(initial_angle)),'FontSize',14);
+
+for i = 1:length(t)
+
+    % Update pendulum position
+    x = x0 + L*sin(theta_sol(i));
+    y = y0 - L*cos(theta_sol(i));
+
+    hold on;
+
+    % string
+    plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
+    % ball
+    plot(x, y, 'ro', 'MarkerSize', 20);
+
+
+    drawnow;
+    pause(0.1);
+    hold off;
+    cla;
+end
+

+ 62 - 0
classwork/pendulum_variablelength.m

@@ -0,0 +1,62 @@
+clc; clear all; close all;
+
+% Constants
+g = 9.81;
+L = 2;
+omega = 1.5;
+A = 0.4;
+initial_angle = deg2rad(20);
+initial_v = 0;
+initial_conditions = [initial_angle; initial_v];
+t_span = [0 10];
+
+
+length_time = @(l0, t, A, omega) l0 + A*cos(omega*t);
+dlength_dtime = @(t, A, omega) -A*omega*sin(omega*t);
+length_span = length_time(L, t_span, A, omega);
+
+ode_system = @(t, theta) [
+    theta(2);
+    -(g/length_time(L, t, A, omega))*sin(theta(1)) - (2*dlength_dtime(t, A, omega)/length_time(L, t, A, omega))*theta(2)
+];
+
+[t, theta] = ode45(ode_system, t_span, initial_conditions);
+theta_sol = theta(:, 1);
+dthetadt_sol = theta(:, 2);
+
+figure;
+plot(t, theta_sol);
+
+%-------------------------------------------------
+% Pendulum's base position
+x0 = 0; y0 = 0;
+
+% Animation loop
+figure;
+axis equal;
+xlim([-L, L]);  % Set x-axis limits
+ylim([-L, 0.5]);  % Set y-axis limits
+xlabel('x (m)');
+ylabel('y (m)');
+title(sprintf('Pendulum Animation (Angle = %.0f deg)', rad2deg(initial_angle)),'FontSize',14);
+
+for i = 1:length(t)
+
+    % Update pendulum position
+    x = x0 + length_span(i)*sin(theta_sol(i));
+    y = y0 - length_span(i)*cos(theta_sol(i));
+
+    hold on;
+
+    % string
+    plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
+    % ball
+    plot(x, y, 'ro', 'MarkerSize', 20);
+
+
+    drawnow;
+    pause(1);
+    hold off;
+    cla;
+end
+

+ 11 - 0
drawnow_test.m

@@ -0,0 +1,11 @@
+t = linspace(0, 2*pi, 100);
+
+for i = 1:length(t)
+    x = sin(t(1:i));
+    y = cos(t(1:i));
+
+    plot(x, y, 'b-', 'LineWidth', 2);
+    axis([-1.5 1.5 -1.5 1.5]);
+    drawnow;
+    pause(0.03);
+endfor

+ 5 - 0
exam1/functions/projectile_drag.m

@@ -0,0 +1,5 @@
+function [pos_x, pos_y] = projectile_drag(v0, theta, b, m, t)
+    g = 9.81; %m/s^2
+    pos_x = ((m*v0*cos(theta))/b) * (1 - exp(-(b/m) * t));
+    pos_y = (m/b) * (v0*sin(theta)+((m*g)/b)) * (1-exp(-(b/m)*t)) - ((m*g*t)/b);
+end

+ 14 - 0
exam1/problem1_matrixmanip.m

@@ -0,0 +1,14 @@
+A = [ 2  4 -1  3  5;
+      0 -2  7  1  4;
+      3  5  0 -6  2;
+      8 -1  4  2  0;
+      1  3  5 -2  6];
+
+% Part A
+[A_row, A_col] = find(A==7)
+
+% Part B
+A_submatrix = A(2:4, 2:4)
+
+% Part C
+B = [A(:,1) A(:,3)]

+ 15 - 0
exam1/problem2_projectilemotion.m

@@ -0,0 +1,15 @@
+addpath("functions")
+
+% Initial values
+v0 = 50; % m/s
+theta = 45; % degrees
+b = 0.8; %kg/s
+m = 1.0; %kg
+time = 0:0.05:5;
+
+[x_path, y_path] = projectile_drag(v0, theta, b, m, time);
+plot(x_path, y_path, "Color", "red", "LineWidth", 1.5);
+title("Projectile Motion with Air Drag");
+xlabel("x(m)");
+ylabel("y(m)");
+xlim([x_path(1), x_path(end)]);

+ 8 - 0
exam1/problem3_linearalgebra.m

@@ -0,0 +1,8 @@
+A = [1, 1, 1, 1, 1;
+     1, 1, -3, 0, 0;
+     1, -1, 0, 0, 0;
+     0, 0, 1, 1, -5;
+     0, 1, -2, 1, 0]
+
+B = [3;0;0.2;0;0]
+A \ B

+ 16 - 0
exam1/problem4_eigenvectors.m

@@ -0,0 +1,16 @@
+A = [4 1 1 0;
+     1 6 -2 5;
+     -1 -1 1 1;
+     -3 -1 -1 1];
+
+% Task 1: compute eigenvectors
+[V, D] = eig(A)
+eigen_values = diag(D)
+
+% Task 2: find largest eigenvalue and corresponding eigenvector
+[m, index] = max(eigen_values);
+eigen_vector = V(:, index)
+
+% Task 3: Find eigenvalue 1 and eigenvector
+[m, index] = min(abs(eigen_values))
+eigen_vector = V(:, index)

+ 10 - 0
sound_test.m

@@ -0,0 +1,10 @@
+% Define parameters
+Fs = 8192;            % Sampling frequency (Hz)
+t = 0:1/Fs:2;         % 2 seconds duration
+f = 440;              % Frequency of the tone (440 Hz)
+
+% Generate sine wave
+y = sin(2 * pi * f * t);
+
+% Play the generated tone
+sound(y, Fs)