matlab函数绘图

1.2k words

Night 2


1
2
3
x = linspace(-1,1,100);
y = myfunc1(x);
p=plot(x,y,'g');


1
2
3
4
p.LineWidth = 2;
title('Exercise 2')
xlabel('x')
ylabel('y')




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
x = linspace(0, 2, 100);
y1 = exp(x);
y2 = exp(2*x);
y3 = exp(3*x);

% Compute the polynomial
b = 2; % Choose a value for b
y_poly = myfunc3(x, 1, b, 0); % y = x^2 + b*x + 0`

% Plot the results
figure;
plot(x, y1, x, y2, x, y3, x, y_poly, 'LineWidth', 2); % Use black dashed line for polynomial
xlabel('x');
ylabel('y');
legend('e^x', 'e^{2x}', 'e^{3x}', 'Polynomial');
title('Exponential Curves and Polynomial');
grid on;

% Set y-scale to log
set(gca, 'YScale', 'log');`


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function visualize_polynomial(g_values, h_values, k_values)
x = -10:0.1:10;

for g = g_values
for h = h_values
for k = k_values
% Calculate y values for the current parameters
y = g * (x - k).^2 + h;

% Plot the polynomial with thicker lines
plot(x, y, 'LineWidth', 200, 'DisplayName', sprintf('g=%d, h=%d, k=%d', g, h, k));
hold on;
end
end
end

legend('Location', 'best');
xlabel('x');
ylabel('y');
title('Visualization of Polynomial y = g(x - k)^2 + h');
grid on;
end

1
2
3
4
5
g_values = [1, 2];
h_values = [5, 10];
k_values = [0, 2];

visualize_polynomial(g_values, h_values, k_values);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
a_values = [1, 2, 3]; % 选择不同的 a 值
b_values = [1, 2, 3]; % 选择不同的 b 值

figure;

cmap = jet(length(a_values) * length(b_values));

for i = 1:length(a_values)
for j = 1:length(b_values)
a = a_values(i);
b = b_values(j);

x = linspace(-a, a, 100);
y = linspace(-b, b, 100);

[X, Y] = meshgrid(x, y);

Z = X.^2 / a^2 + Y.^2 / b^2;

contour(X, Y, Z, [1, 1], 'Color', cmap((i-1)*length(b_values)+j, :), 'LineWidth', 2);

hold on;
end
end

title('Contours of x^2 / a^2 + y^2 / b^2 = 1');
xlabel('x');
ylabel('y');

legend('a=1, b=1', 'a=1, b=2', 'a=1, b=3', 'a=2, b=1', 'a=2, b=2', 'a=2, b=3', 'a=3, b=1', 'a=3, b=2', 'a=3, b=3');

grid on;

hold off;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
figure;

cmap = jet(length(a_values) * length(b_values));

for i = 1:length(a_values)
for j = 1:length(b_values)
a = a_values(i);
b = b_values(j);

x_range = sqrt(10 * b / a) * [-1, 1];
x = linspace(x_range(1), x_range(2), 100);

y = a * x.^2 + b;

plot(x, y, 'Color', cmap((i-1)*length(b_values)+j, :), 'LineWidth', 2);

hold on;
end
end

title('Parabolic Curves y = a * x^2 + b');
xlabel('x');
ylabel('y');

legend('a=1, b=1', 'a=1, b=2', 'a=1, b=3', 'a=2, b=1', 'a=2, b=2', 'a=2, b=3', 'a=3, b=1', 'a=3, b=2', 'a=3, b=3');

grid on;

hold off;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
a_values = [1, 2, 3]; % 选择不同的 a 值
b_values = [1, 2, 3]; % 选择不同的 b 值

figure;

cmap = jet(length(a_values) * length(b_values));

for i = 1:length(a_values)
for j = 1:length(b_values)
a = a_values(i);
b = b_values(j);

x = linspace(-10, 10, 100);
y = linspace(-10, 10, 100);

[X, Y] = meshgrid(x, y);

Z = (X.^2 / a^2) - (Y.^2 / b^2);

contour(X, Y, Z, [1, 1], 'Color', cmap((i-1)*length(b_values)+j, :), 'LineWidth', 2);

hold on; % 保持图形窗口
end
end

title('Contours of Hyperbolic Curves');
xlabel('x');
ylabel('y');

legend('a=1, b=1', 'a=1, b=2', 'a=1, b=3', 'a=2, b=1', 'a=2, b=2', 'a=2, b=3', 'a=3, b=1', 'a=3, b=2', 'a=3, b=3');

grid on;
hold off;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = 1;
b = 2;
t = linspace(0, 4*pi, 1000);

x = a * cos(t);
y = a * sin(t);
z = b * t;

figure;
plot3(x, y, z);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Helix with a=1, b=2');
grid on;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
a = 1;
b = 2;
figure;

x = linspace(-a, a, 100);
y = linspace(-b, b, 100);
[X, Y] = meshgrid(x, y);
Z = X.^2 / a^2 + Y.^2 / b^2;

contour(X, Y, Z, [1, 1]);

title('Ellipse with a=1, b=2');
xlabel('x');
ylabel('y');

grid on;
axis equal;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
x = linspace(-2, 2, 100);
y = linspace(-2, 2, 100);
[X, Y] = meshgrid(x, y);

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

figure;

subplot(1, 3, 1);
surf(X, Y, Z);
shading flat;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Flat Shading');
colorbar;

subplot(1, 3, 2);
surf(X, Y, Z);
shading faceted;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Faceted Shading');
colorbar;

subplot(1, 3, 3);
surf(X, Y, Z);
shading interp;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Interpolated Shading');
colorbar;


1
2
3
4
5
6
7
8
9
10
11
12
x = linspace(-2, 2, 100);
y = linspace(-2, 2, 100);
[X, Y] = meshgrid(x, y);

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

figure;
[C, h] = contour(X, Y, Z, [1, 2, 3]);
xlabel('X');
ylabel('Y');
title('Contour Plot with Height Labels');
clabel(C, h);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
x = linspace(-3, 3, 100);
y = linspace(-3, 3, 100);

a_values = [1, 2, 3];
b_values = [1, 2, 3];
c_values = [1, 2, 3];

[X, Y] = meshgrid(x, y);

figure;

for i = 1:length(a_values)
for j = 1:length(b_values)
for k = 1:length(c_values)

Z = c_values(k) * ((X.^2 / a_values(i)^2) + (Y.^2 / b_values(j)^2));

subplot(length(a_values), length(b_values), (i-1)*length(b_values) + j);

surf(X, Y, Z);

xlabel('X');
ylabel('Y');
zlabel('Z');
title(['a=', num2str(a_values(i)), ', b=', num2str(b_values(j)), ', c=', num2str(c_values(k))]);
end
end
end

sgtitle('Surfaces with Different a, b, and c Values');



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
x = linspace(-5, 5, 100);
y = linspace(-5, 5, 100);
z = linspace(-5, 5, 100);

[X, Y, Z] = meshgrid(x, y, z);

a_values = [1, 2, 3];
b_values = [1, 2, 3];
c_values = [1, 2, 3];

total_combinations = length(a_values) * length(b_values) * length(c_values);

num_rows = ceil(sqrt(total_combinations));
num_cols = ceil(total_combinations / num_rows);

figure;

for i = 1:length(a_values)
for j = 1:length(b_values)
for k = 1:length(c_values)
if (i-1)*length(b_values)*length(c_values) + (j-1)*length(c_values) + k > total_combinations
break;
end

a = a_values(i);
b = b_values(j);
c = c_values(k);

lhs_value = surface_equation(X, Y, Z, a, b, c);

subplot(num_rows, num_cols, (i-1)*length(b_values)*length(c_values) + (j-1)*length(c_values) + k);
p = patch(isosurface(X, Y, Z, lhs_value, 0));
set(p, 'FaceColor', 'blue', 'EdgeColor', 'none');
xlabel('X');
ylabel('Y');
zlabel('Z');
title(['a=', num2str(a), ', b=', num2str(b), ', c=', num2str(c)]);
axis equal;
grid on;
view(3);
end
end
end

sgtitle('Surface Visualization for Different a, b, and c');


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
u = linspace(0, pi, 100);
v = linspace(0, 2*pi, 100);

a_values = [1, 2, 3];
r_values = [0.5, 1, 1.5];

figure;

plot_counter = 1;

for i = 1:length(a_values)
for j = 1:length(r_values)
[X, Y, Z] = parametric_surface(u, v, a_values(i), r_values(j));

subplot(length(a_values), length(r_values), plot_counter);
surf(X, Y, Z);
xlabel('X');
ylabel('Y');
zlabel('Z');
title(['a=', num2str(a_values(i)), ', r=', num2str(r_values(j))]);
axis equal;
grid on;

plot_counter = plot_counter + 1;
end
end

sgtitle('Surface Visualization for Different a and r');

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
a = 5;
b = 4;
c = 3;

[x, y, z] = meshgrid(linspace(-a, a, 100), linspace(-b, b, 100), linspace(-c, c, 100));

ellipsoid_equation = (x / a).^2 + (y / b).^2 + (z / c).^2;

figure;
isosurface(x, y, z, ellipsoid_equation, 1);
axis equal;
xlabel('x');
ylabel('y');
zlabel('z');
title('Watermelon-shaped Ellipsoid');
grid on;
Comments