0% found this document useful (0 votes)
2 views2 pages

Convex Functions Visualization Exercise

This document describes Exercise 2.1, which involves visualizing different types of functions, specifically focusing on a quadratic function that is convex. It includes code for plotting the function, testing its convexity using specific points, and displaying the results in a graphical format. The exercise aims to illustrate the properties of convex functions through graphical representation.

Uploaded by

mekidkrimo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Convex Functions Visualization Exercise

This document describes Exercise 2.1, which involves visualizing different types of functions, specifically focusing on a quadratic function that is convex. It includes code for plotting the function, testing its convexity using specific points, and displaying the results in a graphical format. The exercise aims to illustrate the properties of convex functions through graphical representation.

Uploaded by

mekidkrimo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

%% PART 2: CONVEX FUNCTIONS​


%% Exercise 2.1: Visualization of Different Types of Functions​
fprintf('\n======== EXERCISE 2.1: TYPES OF FUNCTIONS ==========\n');​

% Create a new figure window for plotting​
figure('Name', 'Types of Functions', 'Position', [100, 100, 1200,
800]);​
x = linspace(-3, 3, 200); % Define the x-axis range​

% Test points for graphical verification of convexity​
x1_test = -2;​
x2_test = 2;​
lambda_test = linspace(0, 1, 20); % Parameter for the line segment​

%% 1. Quadratic Function (CONVEX)​
subplot(2,2,1); % Create a subplot in a 2x2 grid at position 1​
f1 = x.^2 + 2.*x + 1; % Define the quadratic function​
plot(x, f1, 'b-', 'LineWidth', 2, 'DisplayName', 'Function f(x)');​
hold on; % Hold the current plot to add more elements​

% --- Graphical Convexity Test ---​
% Calculate the y-values for the test points on the function​
y1 = x1_test.^2 + 2*x1_test + 1;​
y2 = x2_test.^2 + 2*x2_test + 1;​

% Define the line segment connecting the two test points (y_seg)​
% and the corresponding function values under that segment (f_seg)​
x_seg = lambda_test*x1_test + (1-lambda_test)*x2_test;​
y_seg = lambda_test*y1 + (1-lambda_test)*y2; % Chord connecting
(x1,y1) and (x2,y2)​
f_seg = x_seg.^2 + 2*x_seg + 1; % Actual function values below the
chord​

% Plot the test points as red circles​
plot([x1_test, x2_test], [y1, y2], 'ro', 'MarkerSize', 8,
'MarkerFaceColor', 'r', 'DisplayName', 'Test Points');​

% Plot the line segment (chord) in red​
plot(x_seg, y_seg, 'r-', 'LineWidth', 2, 'DisplayName', 'Segment');​

% Plot the actual function values under the segment as green dots​
plot(x_seg, f_seg, 'g.', 'MarkerSize', 8, 'DisplayName', 'f(x) on
segment interval');​

% Add plot details​
title('f(x) = x^2 + 2x + 1 (CONVEX)', 'FontSize', 12);​
xlabel('x');​
ylabel('f(x)');​
grid on;​
legend('Location', 'best');​
hold off;​

You might also like