필터 지우기
필터 지우기

How to plot a function in a for loop?

조회 수: 3 (최근 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by