필터 지우기
필터 지우기

Bezier Curve problem inside for loop

조회 수: 2 (최근 30일)
Michael Jacobson
Michael Jacobson 2018년 3월 10일
댓글: Arthur Vidal 2018년 3월 11일
Hi, I have a problem when generating multiple points from two x & y functions inside a for loop. If I use the plot function to plot x and y, then the curve will not appear. If I use the scatter function it will plot each point though, but I want a smooth curve.
figure; hold on
for u = 0:.01:10
x = (1-u)^3*1+3*(1-u)^2*u*3+3*(1-u)*u^2*5+u^3*8;
y = (1-u)^3*1+3*(1-u)^2*u*0.5+3*(1-u)*u^2*2.5+u^3*2;
plot(x,y)
scatter(A(1,:),A(2,:))
xlim([0 10])
ylim([0 4])
end
hold off
Also, is there already a built-in function which will achieve what I'm trying to do? If not what would be the best way to create a function to use for future reference?

채택된 답변

Arthur Vidal
Arthur Vidal 2018년 3월 11일
Hi Michael,
For your case, for is not recommendded. Instead, use vectors and dot operations to get your code working well. The following code should work.
figure; hold on u = 0:.01:10; x = (1-u).^3*1 + 3*(1-u).^2.*u*3 + 3*(1-u).*u.^2*5 + u.^3*8; y = (1-u).^3*1 + 3*(1-u).^2.*u*0.5 + 3*(1-u).*u.^2*2.5 + u.^3*2; plot(x,y) xlim([0 10]) ylim([0 4]) hold off
  댓글 수: 2
Michael Jacobson
Michael Jacobson 2018년 3월 11일
Thank you, the code works perfectly. Can you explain the logic a bit more though? you say use vectors and dot operations. When I create a range of numbers for variable u, does that mean it is now a size(1,i) vector where i is determined by the increment of the range? If u is a vector does that mean when I input it into the function for x & y, then x & y are now vectors with the same size of u? I don't understand where the dot operation is being conducted. Also, is the for loop incorrect because x & y act as constants with individual u values when implemented in a for loop? and the plot function is meant to pass a vector if I want to generate a curve?
Arthur Vidal
Arthur Vidal 2018년 3월 11일
The way your did first, only one value was being calculated per iteration and you were plotting a single point, not a set of points. Plot needs to have two vectors and it connects all the points with a curve. The dot operation is necessary to work with vectors. Dot operation means that you are multiplying or powering element by element, not the whole vector or matrix.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by