Challenge

Project 1 - Data Handling and Interpolation

project --1

% Get user input for mass (m) and spring constant (k)

prompt = {'Enter mass (m):', 'Enter spring constant (k):'};

dlgtitle = 'Input';

dims = [1 35];

defaults = {'1', '1'};

input_data = inputdlg(prompt, dlgtitle, dims, defaults);

% Extract values of m and k from user input

m = str2double(input_data{1});

k = str2double(input_data{2});

% Define the simple harmonic oscillator equation as a system of first-order ODEs

ode_func = @(t, y) [y(2); -k/m * y(1)];

% Initial conditions: displacement (x0) and velocity (v0)

x0 = 1;

v0 = 0;

initial_conditions = [x0; v0];

% Time span for the simulation

tspan = [0 10];

% Solve the ODE using ode45

[t_ode45, y_ode45] = ode45(ode_func, tspan, initial_conditions);

% Solve the ODE using ode23

[t_ode23, y_ode23] = ode23(ode_func, tspan, initial_conditions);

% Plot the results

figure;

subplot(3,1,1);

plot(t_ode45, y_ode45(:,1), 'b-', t_ode23, y_ode23(:,1), 'r--');

title('Displacement (x)');

legend('ode45', 'ode23');

subplot(3,1,2);

plot(t_ode45, y_ode45(:,2), 'b-', t_ode23, y_ode23(:,2), 'r--');

title('Velocity (\dot{x})');

legend('ode45', 'ode23');

subplot(3,1,3);

acceleration_ode45 = -k/m * y_ode45(:,1);

acceleration_ode23 = -k/m * y_ode23(:,1);

plot(t_ode45, acceleration_ode45, 'b-', t_ode23, acceleration_ode23, 'r--');

title('Acceleration (\ddot{x})');

legend('ode45', 'ode23');

sgtitle(['Simple Harmonic Oscillator (m = ' num2str(m) ', k = ' num2str(k) ')']);

% Explanation

disp('Comparison of ode45 and ode23:');

disp('- ode45 is generally more accurate and robust for stiff and non-stiff ODEs.');

disp('- ode23 is suitable for non-stiff problems but may be less accurate for stiff problems.');

disp('- In this example, both solvers provide similar results, but ode45 is commonly preferred for its versatility.');

project 2

% Load altitude vs temperature data from the International Standard Atmosphere (ISA) table

data = load('ISA_table.txt');

altitude = data(:, 1);  % Altitude in meters

temperature = data(:, 2);  % Temperature in degrees Celsius

% Use polyfit to determine a regression model: T = f(h)

degree = 2;  % Choose the degree of the polynomial

coefficients = polyfit(altitude, temperature, degree);

% Evaluate the model using polyval

evaluated_temperature = polyval(coefficients, altitude);

% Determine the goodness of fit

goodness = goodnessOfFit(altitude, temperature, 'polyval', evaluated_temperature);

% Generate comparative plot

figure;

plot(altitude, temperature, 'o', 'DisplayName', 'Original Data');

hold on;

plot(altitude, evaluated_temperature, 'r-', 'DisplayName', 'Polyfit Model');

xlabel('Altitude (m)');

ylabel('Temperature (°C)');

title('International Standard Atmosphere (ISA)');

legend('Location', 'northwest');

grid on;

hold off;

% Store polyval evaluated data into a text file

polyval_data = [altitude, evaluated_temperature];

dlmwrite('polyval_evaluated_data.txt', polyval_data, '\t');

disp('Polyfit and Polyval completed. Polyval evaluated data stored in polyval_evaluated_data.txt.');

disp(['Goodness of Fit (R-squared): ' num2str(goodness.rsquare)]);

project 3

% Define the function

fun = @(x) sin(x.^2) + cos(x) - 2*sin(x).*cos(x);

% Generate x values for plotting

x_values = linspace(-3*pi, 3*pi, 1000);

% Evaluate the function

y_values = fun(x_values);

% Generate the plot

figure;

plot(x_values, y_values, 'LineWidth', 2);

xlabel('x');

ylabel('y');

title('Plot of y = sin(x^2) + cos(x) - 2*sin(x)*cos(x)');

grid on;

% Find local minima using fminsearch

local_minima = fminsearch(fun, [-2*pi, 0, 2*pi]);

% Find global maximum using max

[global_max, idx_max] = max(y_values);

x_global_max = x_values(idx_max);

% Highlight local minima and global maximum on the plot

hold on;

plot(local_minima, fun(local_minima), 'ro', 'MarkerSize', 10, 'DisplayName', 'Local Minima');

plot(x_global_max, global_max, 'go', 'MarkerSize', 10, 'DisplayName', 'Global Maximum');

hold off;

% Add text labels to the highlighted points

gtext('Local Minima', 'Color', 'r');

gtext('Global Maximum', 'Color', 'g');

legend('Location', 'Best');

project ---4

The contour command in MATLAB is used to create contour plots or filled contour plots of two-dimensional data. Contour plots are useful for visualizing the level curves or surfaces of a function of two variables. The contour function can be used to create both line contours and filled contours, providing a visual representation of the variation in a function across a 2D plane.

Here's a brief note and an example illustrating the use of the contour command:

contour Command Syntax:.

contour(X, Y, Z, levels)

  • X, Y: Vectors or matrices representing the x and y coordinates of the grid.
  • Z: Matrix representing the function values at each grid point.
  • levels: Optional parameter specifying the contour levels to plot

Example:

% Example data

[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);

Z = X.^2 + Y.^2;

% Create a contour plot

figure;

contour(X, Y, Z, 'ShowText', 'on');

title('Contour Plot of Z = X^2 + Y^2');

xlabel('X-axis');

ylabel('Y-axis');

colorbar; % Display color scale

In this example, we generate a meshgrid of X and Y values and calculate Z values based on the function �=�2+�2Z=X2+Y2. The contour function is then used to create a contour plot of this 2D function. The 'ShowText', 'on' parameter displays the contour values on the plot, and colorbar adds a color scale to the plot.

This will produce a contour plot with concentric circles representing the level curves of the function �=�2+�2Z=X2+Y2. You can adjust the range, step size, and function according to your specific requirements.

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.