Challenge

Project 1 - Data Handling and Interpolation

Project 1 :

  • Solve the simple harmonic oscillator equation [m . dx² / dt² = -k.x ] for a given m (mass) and k (spring constant) values using ‘ode45’ and ‘ode23’ solver in MATLAB.
  • Plot x, x_dot (ẋ) & x_dot_dot (ẍ).
  • Use the input dialog box to get ‘m’ & ‘k’ values from the user.
  • Compare ‘ode45’ and ‘ode23’ plots and explain.

Solution

Matlab code

function simple_harmonic_oscillator()
% Get user input for mass (m) and spring constant (k)
prompt = {'Enter mass (m):', 'Enter spring constant (k):'};
dlgtitle = 'Input';
dims = [1 35];
definput = {'1', '1'};
input = inputdlg(prompt, dlgtitle, dims, definput);

m = str2double(input{1});
k = str2double(input{2});

% Define the differential equation
fun = @(t, y) [y(2); -k / m * y(1)];

% Initial conditions: x(0) = 0, x_dot(0) = 1
y0 = [0; 1];

% Time span for the simulation
tspan = [0, 10];

% Solve using ode45 and ode23
[t45, sol45] = ode45(fun, tspan, y0);
[t23, sol23] = ode23(fun, tspan, y0);

% Extract the solutions
x45 = sol45(:, 1);
x_dot45 = sol45(:, 2);
x23 = sol23(:, 1);
x_dot23 = sol23(:, 2);

% Plot the results
figure;

% Position vs. Time
subplot(3, 1, 1);
plot(t45, x45, 'b', t23, x23, 'r');
xlabel('Time (s)');
ylabel('Position (x)');
legend('ode45', 'ode23');
title('Position vs. Time');

% Velocity vs. Time
subplot(3, 1, 2);
plot(t45, x_dot45, 'b', t23, x_dot23, 'r');
xlabel('Time (s)');
ylabel('Velocity (ẋ)');
legend('ode45', 'ode23');
title('Velocity vs. Time');

% Acceleration vs. Time
subplot(3, 1, 3);
x_dot_dot45 = -k / m * x45;
x_dot_dot23 = -k / m * x23;
plot(t45, x_dot_dot45, 'b', t23, x_dot_dot23, 'r');
xlabel('Time (s)');
ylabel('Acceleration (ẍ)');
legend('ode45', 'ode23');
title('Acceleration vs. Time');
end

Result

PROJECT 2.

% Step 1: Load Altitude vs. Temperature Data

% Assuming you have copied the data from the ISA tables into an Excel file.

% Replace 'path/to/isa_data.xlsx' with the actual path to your Excel file.

[altitude, temperature] = xlsread('miniproject21.xlsx');

altitude = altitude(:, 1);

temperature = temperature(:, 2);

% Step 2: Use 'polyfit' to Determine a Regression Model
degree = 3; % You can choose a different degree based on the data characteristics.
coefficients = polyfit(altitude, temperature, degree);
% Step 3: Evaluate the Model using 'polyval'
altitude_eval = min(altitude):1000:max(altitude); % Choose the range for evaluation.
temperature_eval = polyval(coefficients, altitude_eval);
% Step 4: Determine the Goodness of Fit
gof = goodnessOfFit(altitude, temperature, polyval(coefficients, altitude), 'MSE');
% Step 5: Generate Comparative Plot
figure;
plot(altitude, temperature, 'o', 'DisplayName', 'Original Data');
hold on;
plot(altitude_eval, temperature_eval, 'r', 'DisplayName', 'Regression Model');
xlabel('Altitude (m)');
ylabel('Temperature (°C)');
legend('Location', 'best');
title('Altitude vs. Temperature');
% Step 6: Store 'polyval' Evaluated Data in a Text File
evaluated_data = [altitude_eval', temperature_eval'];
dlmwrite('evaluated_data.txt', evaluated_data, 'delimiter', '\t', 'precision', '%.4f');
disp('Evaluated data saved to evaluated_data.txt');

PROJECT 3.

function find_extrema()
% Step 1: Create the function
f = @(x) sin(x.^2) + cos(x) - 2 * sin(x) .* cos(x);

% Step 2: Generate the plot of the function
x = linspace(-5*pi, 5*pi, 1000);
y = f(x);

figure;
plot(x, y);
xlabel('x');
ylabel('y');
title('Plot of y = sin(x^2) + cos(x) - 2sin(x)cos(x)');

% Step 3: Use 'fminsearch' to find local minima
local_minima_x = [];
local_minima_y = [];
for i = 1:length(x)-1
x0 = x(i);
x1 = x(i+1);
local_minima = fminsearch(f, [x0, x1]);
if local_minima(1) >= x(i) && local_minima(1) <= x(i+1)
local_minima_x(end+1) = local_minima(1);
local_minima_y(end+1) = f(local_minima(1));
end
end

% Step 4: Use 'max' to find the global maximum
[global_max_y, global_max_index] = max(y);
global_max_x = x(global_max_index);

% Step 5: Highlight the local minima and global maxima on the plot
hold on;
plot(local_minima_x, local_minima_y, 'ro', 'MarkerSize', 6, 'DisplayName', 'Local Minima');
plot(global_max_x, global_max_y, 'go', 'MarkerSize', 8, 'DisplayName', 'Global Maxima');
legend('Location', 'best');

% Add text labels near the points
text(local_minima_x + 1, local_minima_y, sprintf('Minima: x = %.2f, y = %.2f', local_minima_x, local_minima_y), 'FontSize', 8);
text(global_max_x + 1, global_max_y, sprintf('Maxima: x = %.2f, y = %.2f', global_max_x, global_max_y), 'FontSize', 8);
end

result

PROJECT 4

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.