Borrar filtros
Borrar filtros

How to plot a function in a for loop?

3 visualizaciones (últimos 30 días)
Mahmoud Abbas
Mahmoud Abbas el 25 de Feb. de 2022
Comentada: Mahmoud Abbas el 25 de Feb. de 2022
n=3;
for i=1:n+1 %this loop returns 4 curves, I want to plot them on a single graph
f{i} = @Bezier;
B=Bezier(n,i-1);
end
%I also want to plot the sum of the 4 curves (i.e. the curve that was generated at i=1 + the one at i=2...etc) (Their sums should be equal 1 across the range)
%This is the function code mentioned above
function [B]=Bezier(n,i)
figure; hold on
u=0:0.001:1;
B=factorial(n)/(factorial(i)*factorial(n-i))*u.^i.*(1-u).^(n-i); %bezier curves function
plot(u,B,'.')
end
%I can combine the curves in the function file but I don't know how to do it when I am calling the function
%and the sum of all graphs at any point should equal 1

Respuesta aceptada

Torsten
Torsten el 25 de Feb. de 2022
Editada: Torsten el 25 de Feb. de 2022
function main
n = 3;
u = 0:0.001:1;
for i = 1:n+1 %this loop returns 4 curves, I want to plot them on a single graph
B{i} = Bezier(u,n,i-1);
end
plot(u,[B{1};B{2};B{3};B{4};B{1}+B{2}+B{3}+B{4}])
end
function B = Bezier(u,n,i)
B = factorial(n)/(factorial(i)*factorial(n-i))*u.^i.*(1-u).^(n-i); %bezier curves function
end

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by