How to make a function generate random outputs using variables with given values?

1 view (last 30 days)
I have a 3D plot that uses the following equations for x,y,z:
t = linspace(0,2.*pi,1000);
x = 4.*cos(t).*sin(2.*t).*cos(10*t);
y = 7.*sin(t).*cos(3.*t);
z = t;
and the following codes to plot them:
figh = figure;
for k=1:length(t)
clf
t_k = t(k);
x_k = x(k);
y_k = y(k);
z_k = z(k);
plot3(x_k, y_k, z_k, 'gd', 'LineWidth', 2.5, 'MarkerSize', 12)
hold on
plot3(0.99*x_k, 0.99*y_k, 0.99*z_k, 'ro', 'LineWidth', 2.5, 'MarkerSize', 10)
hold on
plot3(0.98*x_k, 0.98*y_k, 0.98*z_k, 'bs', 'LineWidth', 2.5, 'MarkerSize', 10)
hold on
plot3(x, y, z, 'k-', 'LineWidth', 2);
end
My question is is there any way of producing a designated number of randomly generated outputs using this code? But for example for each output, things such as plot colour, marker appearance, size and width change?

Accepted Answer

Voss
Voss on 27 Jan 2022
t = linspace(0,2.*pi,1000);
x = 4.*cos(t).*sin(2.*t).*cos(10*t);
y = 7.*sin(t).*cos(3.*t);
z = t;
N = 3;
figh = zeros(1,N);
for v = 1:N
figh(v) = plot_xyzt(x,y,z,t);
end
function figh = plot_xyzt(x,y,z,t)
marker_size = randi(20,1,3);
line_width = randi(10,1,4);
color = rand(4,3);
marker = 'hop*^svd<>+x.';
marker = marker(randi(numel(marker),1,3));
figh = figure;
for k=1:length(t)
clf
% t_k = t(k);
x_k = x(k);
y_k = y(k);
z_k = z(k);
plot3(x_k, y_k, z_k,'Color',color(1,:),'LineWidth',line_width(1),'MarkerSize',marker_size(1))
hold on
plot3(0.99*x_k, 0.99*y_k, 0.99*z_k,'Color',color(2,:),'Marker',marker(2),'LineWidth',line_width(2),'MarkerSize',marker_size(2))
hold on
plot3(0.98*x_k, 0.98*y_k, 0.98*z_k,'Color',color(3,:),'Marker',marker(3),'LineWidth',line_width(3),'MarkerSize',marker_size(3))
hold on
plot3(x, y, z,'Color',color(4,:),'LineWidth',line_width(4))
drawnow()
end
end

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!