Project 1 - Data Handling and Interpolation
1) Ans-
Script:
%Harmonic oscillator equation [m.dx^2/dt^2=-k.x]
m = input('Enter the mass (m): ');
k = input('Enter the spring constant (k): ');
ode = @(t, x) [x(2); -k/m * x(1)];
x0 = 1; % Initial position
v0 = 0; % Initial velocity
tspan=[0,10];%Time span
[t45, sol45]=ode45(ode, tspan, [x0; v0]);
[t23, sol23]=ode23(ode, tspan, [x0; v0]);
x45 = sol45(:, 1);
v45 = sol45(:, 2);
x23 = sol23(:, 1);
v23 = sol23(:, 2);
subplot(2, 2, 1);
plot(t45, x45, 'b', 'LineWidth', 2);
title('ode45 - Position');
xlabel('Time');
ylabel('Position');
subplot(2, 2, 2);
plot(t45, v45, 'r', 'LineWidth', 2);
title('ode45 - Velocity');
xlabel('Time');
ylabel('Velocity');
subplot(2, 2, 3);
plot(t23, x23, 'b', 'LineWidth', 2);
title('ode23 - Position');
xlabel('Time');
ylabel('Position');
subplot(2, 2, 4);
plot(t23, v23, 'r', 'LineWidth', 2);
title('ode23 - Velocity');
xlabel('Time');
ylabel('Velocity');
sgtitle('Solution of Second-Order ODE');
Command window:
Harmonic_oscillator_equation
Enter the mass (m): 2
Enter the spring constant (k): 50
Harmonic_oscillator_equation
Enter the mass (m): 5
Enter the spring constant (k): 50
Harmonic_oscillator_equation
Enter the mass (m): 10
Enter the spring constant (k): -50
2) Ans-
Script:
elevation=[-2000,-1500,-1000,-500,0,500,1000,1500,2000,2500,3000,3500,4000,4500,5000,5500,6000,6500,7000,7500,8000,8500,9000,9500,10000,10500,11000,11500,12000,12500,13000,13500,14000,14500,15000,15500,16000,16500,17000,17500,18000,18500,19000,19500,20000,22000,24000,26000,28000,30000,];
temperature=[301.2,297.9,294.7,291.4,288.15,284.9,281.7,278.4,275.2,271.9,268.7,265.4,262.2,258.9,255.7,252.4,249.2,245.9,242.7,239.5,236.2,233,229.7,226.5,223.3,220,216.8,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,216.7,218.6,220.6,222.5,224.5,226.5];
degree = 3;
coefficients=polyfit(elevation, temperature, degree);
temperature_predicted=polyval(coefficients, elevation);
residuals=(temperature - temperature_predicted);
SSR=sum(residuals.^2);
SST=sum((temperature - mean(temperature)).^2);
goodness=(1 - SSR / SST);
fprintf('Goodness of Fit (R^2): %.4f\n', goodness);
figure;
scatter(elevation, temperature, 'o', 'DisplayName', 'Loaded Data');
hold on;
plot(elevation, temperature_predicted, 'r-', 'DisplayName', 'Regression Model');
xlabel('Elevation (m)');
ylabel('Temperature (K)');
legend('Location', 'southeast');
title('Comparative Plot');
grid on;
evaluated_data = [elevation', temperature_predicted'];
dlmwrite('evaluated_data.txt', evaluated_data, 'delimiter', '\t');
Command window:
loadvstemperature
Goodness of Fit (R^2): 0.9847
3) Ans-
Script:
evaluate_expression = @(x) sin(x.^2) + cos(x) - 2.*sin(x).*cos(x);
x = input('Enter the value of x: ');
y = evaluate_[removed]x);
fprintf('For x = %.4f, y = %.4f\n', x, y);
options = optimset('fminsearch');
options.Display = 'off';
local_minima_x = fminsearch(evaluate_expression, 0, options);
global_maxima_x = fminbnd(@(x) -evaluate_[removed]x), -10, 10);
x_range = linspace(-10, 10, 1000); �just the range as needed
y_range = evaluate_[removed]x_range);
figure;
plot(x_range, y_range, 'b');
hold on;
plot(local_minima_x, evaluate_[removed]local_minima_x), 'ro', 'DisplayName', 'Local Minima');
plot(global_maxima_x, evaluate_[removed]global_maxima_x), 'go', 'DisplayName', 'Global Maxima');
xlabel('x');
ylabel('y');
title('Plot of y = sin(x^2) + cos(x) - 2*sin(x)*cos(x)');
grid on;
legend('Location', 'best');
hold off;
fprintf('Local Minima: (%.4f, %.4f)\n', local_minima_x, evaluate_[removed]local_minima_x));
fprintf('Global Maxima: (%.4f, %.4f)\n', global_maxima_x, evaluate_[removed]global_maxima_x));
Command window:
trigonometry2
Enter the value of x: 5
For x = 5.0000, y = 0.6953
Local Minima: (0.6392, 0.2423)
Global Maxima: (5.1868, 2.2497)
4) Ans-
The 'contour' command in MATLAB is utilized to create contour plots, a graphical representation of the level curves of a 3D surface on a 2D plane. It is especially useful for visualizing functions of two variables.
Imagine you have a function that depends on two variables, let's say x and y, and you want to see how this function behaves on a flat piece of paper. The contour command in MATLAB helps you with that. It creates a map of this function's values on the paper.
For a example,
[x, y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = x.^2 + y.^2;
figure;
contour(x, y, Z);
xlabel('X');
ylabel('Y');
title('Contour Plot of Z = x^2 + y^2');
colorbar; % Display a colorbar to indicate contour levels
This MATLAB code creates a visual map. We start with a grid of points on paper. Each point gets a number based on its position. Lines are drawn to connect points with similar numbers, showing where the numbers are close. Labels, a title, and a color scale make the map easy to understand. The result is a visual representation of data patterns.