What makes Matlab and Octave different?Matlab and Octave are the two most popular statistical programming languages today. They offer researchers and data scientists a host of tools for statistical analysis to help them produce the most desirable results from their data. But it can be quite overwhel...
clc, clear all, close all
% Load dat file
load crazy_func
% Plot the curve
figure
subplot(1,2,1);
plot(eks, why), grid on
xlabel('eks');
ylabel('why');
title('why vs. eks');
% Determine d(why)/d(eks)
N = length(why); % length of data
% The length of d(why)/d(eks) will be N - 1
for i = 1:N-1
dwhy(i) = why(i+1)-why(i);
deks(i) = eks(i+1) - eks(i);
end
% Now, compute d(why)/d(eks)
dwhydeks = dwhy./deks;
% Plot the resulting curve
subplot(1,2,2)
plot(eks(1:N-1), dwhydeks), grid on
xlabel('deks');
ylabel('dwhy');
title('dwhy vs. deks');
%% NOTE FOR THE REPORT:
% eks < 20 -> why is a sine function, so its derivative must be a cosine
% function
% eks >=20 and eks < 40 -> why is an exponential function so its
% derivative must be also an exponential functionç
% eks >=40 and eks < 60 -> why is a collection of random points so its
% derivative must be also a collection of random points
% eks >= 60 and eks < 80 -> why is a parabola, so its derivative must be
% a straight line with positive slope
% eks >= 80 and eks < 100 -> why is a line, so its derivative must be a
% constant value
clc, clear all, close all
%% Question 2
% Input values
vxi = [0 -0.2478 -0.4943 -0.7384 -0.9786];
vyi = [4 3.9915 3.9659 3.9234 3.8644];
ti = [0 0.04 0.08 0.12 0.17];
% Initial values
x1 = 4;
y1 = 0;
% Calculate x2, x3, x4, y2, y3, y4
x2 = x1 + vxi(1)*(ti(2)-ti(1));
x3 = x1 + vxi(2)*(ti(3)-ti(2));
x4 = x1 + vxi(3)*(ti(4)-ti(3));
y2 = y1 + vyi(1)*(ti(2)-ti(1));
y3 = y1 + vyi(2)*(ti(3)-ti(2));
y4 = y1 + vyi(3)*(ti(4)-ti(3));
%% Question 3
% Read file into a table
data = readtable('A1_input.txt');
% Do not take into account the first element of the data because that one
% contains the units
for k = 2:size(data,1)
t(k-1) = str2double(data.time{k});
vx(k-1) = str2double(data.vx{k});
vy(k-1) = str2double(data.vy{k});
end
% Display the first four values
[t(1:4)', vx(1:4)', vy(1:4)']
%% Question 4
% Plot vx and vy in the same graph
figure
plot(t, vx, t, vy);
grid on
legend('vx', 'vy');
xlabel('Time (s)');
ylabel('Velocity (m/s)');
grid on
x(1) = x1;
y(1) = y1;
for k = 2:length(t)
x(k) = x1;
y(k) = y1;
for j = 1:k-1
x(k) = x(k) + vx(j)*(t(j+1)-t(j));
y(k) = y(k) + vy(j)*(t(j+1)-t(j));
end
end
% x and y using vectors
% vectors of tj+1 tj
tvec = t(2:end) - t(1:end-1);
xv = cumsum(vx(1:end-1).*tvec);
xv = xv + x1;
xv = [x1, xv];
yv = cumsum(vy(1:end-1).*tvec);
yv = yv + y1;
yv = [y1, yv];
figure
hold on
plot(t, x);
plot(t, y);
plot(t, xv, 'k--', 'linewidth', 3);
plot(t, yv, 'g--', 'linewidth', 3);
grid on
xlabel('Time (s)');
ylabel('Position');
legend('X-position from Q5', 'Y-position from Q5', 'X-position from Q6', 'Y-position from Q6');
figure
plot(xv, yv), grid on
xlabel('X-Position (m)');
ylabel('Y-Position (m)');
file = fopen('output.txt', 'wt');
fprintf(file, 'Time\tx\ty\n');
fprintf(file, '(s)\t(m)\t(m)\n');
for k = 1:length(t)
fprintf(file, '%.3f\t%.3f\t%.3f\n', t(k),xv(k), yv(k));
end
fclose(file);
file = fopen('output2.txt', 'wt');
fprintf(file, 'Time\tx\ty\n');
fprintf(file, '(s)\t(m)\t(m)\n');
fprintf(file, '%.3f\t%.3f\t%.3f\n', t',xv', yv');
fclose(file);