Ans 1:
Defining ODE as function
function dx=mass_eg(t,x,m,k)
dx=zeros(2,1);
dx(1)=x(2);
dx(2)=-k/m * x(1);
end
Now Plotting
clc
clear all;
% User input for m and k
prompt= {'Provide mass m:','Provide constant k'}
dlgtitle= 'User Input';
definput = {'100','3994'};
dims=[1 36];
% Create DialogBox
IP=inputdlg(prompt,dlgtitle,dims,definput);
% Converting string to number
m=str2double(IP(1));
k=str2double(IP(2));
x0=[0.01;0];
ts=[0 5];
[t1,x1]=ode45(@(t, x) mass_eg(t, x, m, k), ts, x0);
[t2,x2]=ode45(@(t, x) mass_eg(t, x, m, k), ts, x0);
% Compute acceleration for ode45
x1_dot_dot = -k/m * x1(:, 1);
% Compute acceleration for ode23
x2_dot_dot = -k/m * x2(:, 1);
figure
plot(t1, x1(:,1), 'r', t2, x2(:,1), 'b');
legend('ode45', 'ode23');
title('Displacement over Time');
xlabel('Time');
grid on;
ylabel('Displacement (x)');
figure
plot(t1, x1(:,2), 'r', t2, x2(:,2), 'b');
legend('ode45', 'ode23');
title('Displacement over Time');
xlabel('Time');
grid on;
ylabel('Displacement (x)');
plot(t1, x1_dot_dot, 'r', t2, x2_dot_dot, 'b');
legend('ode45', 'ode23');
title('Acceleration over Time');
xlabel('Time');
ylabel('Acceleration (ẍ)');
The plot results are shown here
ode45: Provides higher accuracy for smooth solutions but can be slower. It's based on a higher-order Runge-Kutta method.: Faster, especially useful when high precision isn't essential. It’s based on a lower-order Runge-Kutta method.
ode23: Faster, especially useful when high precision isn't essential. It’s based on a lower-order Runge-Kutta method.
But here as we can see both of them are producing same results.
Ans2:
Since in the attached link there is no file or data available. I have taken the ISA data directly at some points.
Script
% Step 1: Load altitude vs temperature data
altitude = [0, 5000, 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000];
temperature = [15, -17.5, -50, -56.5, -56.5, -51.6, -46.6, -37, -22.8, -2.5, -12.5];
% Step 2: Fit polynomial model: T = f(h)
degree = 3; % Degree of polynomial for fitting
coeffs = polyfit(altitude, temperature, degree); % Fit polynomial
% Step 3: Evaluate the model using polyval
evaluatedTemperature = polyval(coeffs, altitude); % Evaluate the model
% Step 4: Determine goodness of fit
goodnessOfFitValue = goodnessOfFit(temperature, evaluatedTemperature);
% Display goodness of fit
fprintf('Goodness of Fit (R²): %.4f\n', goodnessOfFitValue);
% Step 5: Generate comparative plot
figure;
plot(altitude, temperature, 'bo', 'DisplayName', 'Measured Data'); % Original data
hold on;
plot(altitude, evaluatedTemperature, 'r-', 'DisplayName', 'Fitted Model'); % Fitted model
title('Altitude vs Temperature');
xlabel('Altitude (m)');
ylabel('Temperature (°C)');
legend('Location', 'Best');
grid on;
hold off;
% Step 6: Store evaluated data into a text file
outputData = table(altitude', evaluatedTemperature'); % Create a table
writetable(outputData, 'evaluated_temperature_data.txt', 'Delimiter', '\t', 'WriteVariableNames', false); % Save as text file
% Goodness of Fit Function
function rSquared = goodnessOfFit(observed, predicted)
% Calculate R-squared value
ssResidual = sum((observed - predicted).^2); % Residual sum of squares
ssTotal = sum((observed - mean(observed)).^2); % Total sum of squares
rSquared = 1 - (ssResidual / ssTotal); % R-squared calculation
end
Results obtained are attached in word file
An3:
% Define the function
myFunction = @(x) sin(x.^2) + cos(x) - 2*sin(x).*cos(x);
% Generate x values
x = linspace(-10, 10, 1000); % Adjust range and density as needed
y = myFunction(x);
% Plot the function
figure;
plot(x, y, 'b', 'LineWidth', 1.5);
title('Plot of y = sin(x^2) + cos(x) - 2 sin(x) cos(x)');
xlabel('x');
ylabel('y');
grid on;
hold on;
% Find local minima using fminsearch
localMinima = [];
xRange = linspace(-10, 10, 100); % Search in this range
for i = 1:length(xRange)-1
% Use fminsearch to find local minima in each interval
x0 = (xRange(i) + xRange(i+1)) / 2; % Start point in the interval
localMinima(i) = fminsearch(myFunction, x0);
end
% Get unique local minima and their corresponding y values
localMinima = unique(localMinima);
yLocalMinima = myFunction(localMinima);
% Find global maximum in the y values
globalMax = max(y);
globalMaxX = x(find(y == globalMax, 1));
% Plot local minima
plot(localMinima, yLocalMinima, 'ro', 'MarkerFaceColor', 'r', 'DisplayName', 'Local Minima');
text(localMinima, yLocalMinima, arrayfun(@(y) sprintf(' (%.2f, %.2f)', y, myFunction(y)), localMinima, 'UniformOutput', false));
% Plot global maximum
plot(globalMaxX, globalMax, 'go', 'MarkerFaceColor', 'g', 'DisplayName', 'Global Maximum');
text(globalMaxX, globalMax, sprintf(' (%.2f, %.2f)', globalMaxX, globalMax));
% Add legend
legend('y = f(x)', 'Location', 'Best');
% Use gtext to highlight local minima and global maximum
gtext('Local Minima', 'Color', 'r');
gtext('Global Maximum', 'Color', 'g');
hold off;
Results for local Minima & Global Maxima
Ans 4:
The contour command in MATLAB is used to create contour plots, which are graphical representations of three-dimensional data in two dimensions using contour lines. Each contour line represents a constant value of a function over a two-dimensional space. This type of plot is particularly useful for visualizing the relationships between variables, such as in topographic maps or to illustrate gradients in data.
Script
% Define the grid
x = -2:0.1:2;
y = -2:0.1:2;
[X, Y] = meshgrid(x, y);
% Define the function
Z = exp(-X.^2 - Y.^2);
% Create the contour plot
figure;
contour(X, Y, Z);
title('Contour Plot of exp(-X^2 - Y^2)');
xlabel('X');
ylabel('Y');
colorbar;
Graph Obtained