% --- Gradient Vector Field ---
% Define grid for x and y
[X, Y] = meshgrid(-5:0.5:5, -5:0.5:5);

% Define the function f(x, y)
f = X.^2 + Y.^2;

% Compute the gradient (partial derivatives)
[Fx, Fy] = gradient(f, 0.5, 0.5);

% Plot the vector field
figure;
quiver(X, Y, Fx, Fy);
title('Vector Field of the Gradient of f(x, y) = x^2 + y^2');
xlabel('X');
ylabel('Y');
axis tight;

% Save the vector field plot as an image
saveas(gcf, 'vector_field_plot.png');

% Save the grid data and gradient data to a CSV file
gradientData = table(X(:), Y(:), Fx(:), Fy(:), 'VariableNames', {'X', 'Y', 'Fx', 'Fy'});
writetable(gradientData, 'gradient_data.csv');
disp('Vector field plot saved and gradient data saved as gradient_data.csv');

% --- Surface of Revolution ---
% Define the curve y = sqrt(x)
x = linspace(0, 10, 100);
y = sqrt(x);

% Create the surface of revolution by rotating the curve around the x-axis
[X, Z] = meshgrid(x, linspace(0, 2*pi, 100));  % Mesh grid for X and Z
Y = repmat(y, length(Z), 1);                    % Repeat Y values
Y = Y .* cos(Z);                                % Rotation around x-axis
X = repmat(x, length(Z), 1);                    % Repeat X values

% Plot the surface of revolution
figure;
surf(X, Y, Z);
title('Surface of Revolution: y = sqrt(x) rotated around x-axis');
xlabel('X');
ylabel('Y');
zlabel('Z');
colorbar;

% Save the surface plot as an image
saveas(gcf, 'surface_of_revolution.png');

% Save the surface data to a CSV file
surfaceData = table(X(:), Y(:), Z(:), 'VariableNames', {'X', 'Y', 'Z'});
writetable(surfaceData, 'surface_of_revolution_data.csv');
disp('Surface of Revolution plot saved and data saved as surface_of_revolution_data.csv');

% --- 3D Surface, Mesh, and Contour Plots ---
% Define grid for x and y
[X, Y] = meshgrid(-5:0.1:5, -5:0.1:5);

% Define function for Z
Z = sin(X.^2 + Y.^2);

% 3D Surface Plot
figure;
surf(X, Y, Z);
title('3D Surface of f(x, y) = sin(x^2 + y^2)');
xlabel('X');
ylabel('Y');
zlabel('Z');
colorbar;

% Mesh Plot
figure;
mesh(X, Y, Z);
title('Mesh plot of f(x, y) = sin(x^2 + y^2)');
xlabel('X');
ylabel('Y');
zlabel('Z');
colorbar;

% Contour Plot
figure;
contour(X, Y, Z, 20);
title('Contour plot of f(x, y) = sin(x^2 + y^2)');
xlabel('X');
ylabel('Y');
colorbar;

% Save the 3D surface plot as a PNG image
saveas(gcf, 'surface_plot.png');

% Export Data
data = [X(:), Y(:), Z(:)];  % Convert data to a single matrix
writetable(array2table(data, 'VariableNames', {'X', 'Y', 'Z'}), 'function_data.csv');
disp('3D surface plot saved and function data saved as "function_data.csv".');

% --- Mandelbrot Set ---
% Define parameters for Mandelbrot set
maxIter = 100;
xMin = -2; xMax = 1;
yMin = -1.5; yMax = 1.5;
[X, Y] = meshgrid(linspace(xMin, xMax, 800), linspace(yMin, yMax, 800));
C = X + 1i*Y;

% Initialize array for iterations count
Z = zeros(size(C));
iterationCount = zeros(size(C));

% Iterate the Mandelbrot set
for k = 1:maxIter
    Z = Z.^2 + C;
    mask = abs(Z) <= 2;  % Points within the escape radius
    iterationCount(mask) = k;
end

% Plot Mandelbrot set
figure;
imagesc(iterationCount);
colormap jet;
colorbar;
title('Mandelbrot Set');
xlabel('Real');
ylabel('Imaginary');
axis off;

% Save the Mandelbrot set plot as an image
saveas(gcf, 'mandelbrot_plot.png');

% Save the iteration count data to a CSV file
mandelbrotData = table(X(:), Y(:), iterationCount(:), 'VariableNames', {'Real', 'Imaginary', 'IterationCount'});
writetable(mandelbrotData, 'mandelbrot_data.csv');
disp('Mandelbrot set plot saved and iteration count data saved as mandelbrot_data.csv');

% --- Heatmap ---
% Define grid for x and y
[X, Y] = meshgrid(-2*pi:0.1:2*pi, -2*pi:0.1:2*pi);

% Define function f(x, y)
Z = cos(X) .* sin(Y);

% Plot the heatmap
figure;
imagesc(Z);
colorbar;
title('Heatmap of f(x, y) = cos(x) * sin(y)');
xlabel('X');
ylabel('Y');
axis equal;

% Save the heatmap plot as an image
saveas(gcf, 'heatmap_cos_sin.png');

% Save the function values to a CSV file
heatmapData = table(X(:), Y(:), Z(:), 'VariableNames', {'X', 'Y', 'Z'});
writetable(heatmapData, 'heatmap_cos_sin.csv');
disp('Heatmap plot saved and data saved as heatmap_cos_sin.csv');

% --- Eigenvectors of a Random Matrix ---
% Define a random matrix
A = rand(3);

% Calculate eigenvalues and eigenvectors
[V, D] = eig(A);

% Plot the eigenvectors
figure;
quiver3(0, 0, 0, V(1,1), V(2,1), V(3,1), 'r', 'LineWidth', 2);
hold on;
quiver3(0, 0, 0, V(1,2), V(2,2), V(3,2), 'g', 'LineWidth', 2);
quiver3(0, 0, 0, V(1,3), V(2,3), V(3,3), 'b', 'LineWidth', 2);
title('Eigenvectors of a Random Matrix');
xlabel('X');
ylabel('Y');
zlabel('Z');
axis equal;

% Save the eigenvector plot as an image
saveas(gcf, 'eigenvectors_random_matrix.png');

% Save the eigenvectors data to a CSV file
eigenvectorData = table(V(1,:)', V(2,:)', V(3,:)', 'VariableNames', {'X', 'Y', 'Z'});
writetable(eigenvectorData, 'eigenvectors_random_matrix.csv');
disp('Eigenvectors plot saved and data saved as eigenvectors_random_matrix.csv');

% --- 3D Parametric Spiral Curve ---
% Define parametric equations for t
t = linspace(0, 10*pi, 1000);
x = cos(t);
y = sin(t);
z = t;

% Plot the 3D parametric curve
figure;
plot3(x, y, z, 'LineWidth', 2);
title('3D Spiral Curve');
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on;

% Save the 3D plot as an image
saveas(gcf, '3d_spiral_curve.png');

% Save the curve data to a CSV file
spiralData = table(t(:), x(:), y(:), z(:), 'VariableNames', {'t', 'X', 'Y', 'Z'});
writetable(spiralData, 'spiral_curve_data.csv');
disp('3D Spiral Curve plot saved and curve data saved as spiral_curve_data.csv');
Vector field plot saved and gradient data saved as gradient_data.csv
Surface of Revolution plot saved and data saved as surface_of_revolution_data.csv
3D surface plot saved and function data saved as "function_data.csv".
Mandelbrot set plot saved and iteration count data saved as mandelbrot_data.csv
Heatmap plot saved and data saved as heatmap_cos_sin.csv
Eigenvectors plot saved and data saved as eigenvectors_random_matrix.csv
3D Spiral Curve plot saved and curve data saved as spiral_curve_data.csv