How to plot a function in a for loop?

1 vue (au cours des 30 derniers jours)
Mahmoud Abbas
Mahmoud Abbas le 25 Fév 2022
Commenté : Mahmoud Abbas le 25 Fév 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

Réponse acceptée

Torsten
Torsten le 25 Fév 2022
Modifié(e) : Torsten le 25 Fév 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
  1 commentaire
Mahmoud Abbas
Mahmoud Abbas le 25 Fév 2022
Thank you, Torsten!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Least Squares dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by