フィルターのクリア

How to plot a function in a for loop?

4 ビュー (過去 30 日間)
Mahmoud Abbas
Mahmoud Abbas 2022 年 2 月 25 日
コメント済み: Mahmoud Abbas 2022 年 2 月 25 日
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

採用された回答

Torsten
Torsten 2022 年 2 月 25 日
編集済み: Torsten 2022 年 2 月 25 日
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 件のコメント
Mahmoud Abbas
Mahmoud Abbas 2022 年 2 月 25 日
Thank you, Torsten!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLeast Squares についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by