How to write a function of a curve?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi Everyone,
I'm trying to write a function which takes in 4 inputs, not x, and to return a curve as an output.
eg: f(x)= (1-x)^2
Is this possible? Without defining the variable x?
I'm new to Matlab and would appreciate any help, links or simple code to outline how this could be done.
Cheers!
채택된 답변
Hi,
try an anonymous function.
So for example
>> f = @(x,y,z,zz) x + y + z + zz
f =
@(x,y,z,zz)x+y+z+zz
>> f(1,2,3,4)
ans =
10
If your function is too complex, consider writing a function in a seperate m-file (save this in a file called f.m):
function out = f(x,y,z,zz)
out = x + y + z + zz
end
댓글 수: 9
jljl
2012년 8월 28일
Thank you for the extremely fast reply, but I am trying to write a function eg:
function out = f(x,y,z,zz)
% some calculations which end up with (1-t)^2
% or (t^2 +4t)
out = f(t)
end
Is this possible?
Walter Roberson
2012년 8월 28일
Do you mean that you are writing a recursive function ?
jljl
2012년 8월 28일
Yes I am writing a recursive function.
jljl
2012년 8월 28일
I am trying to write the bspline basis function that returns a function of one unknown.
function y = bspline_basis(j,n,t)
% j: interval index
% n: order
% t: knot sequence
% code for the recurrence relation for a bspline basis function
y = zeros(size(x));
if n > 1
b = bspline_basis(j,n-1,t,x);
dn = x - t(j+1);
dd = t(j+n) - t(j+1);
if dd ~= 0 % indeterminate forms 0/0 are deemed to be zero
y = y + b.*(dn./dd);
end
b = bspline_basis(j+1,n-1,t,x);
dn = t(j+n+1) - x;
dd = t(j+n+1) - t(j+1+1);
if dd ~= 0
y = y + b.*(dn./dd);
end
else
y(:) = t(j+1) <= x & x <= t(j+2);
end
% output: eg. the function y(x) = (x - t(j+1))^2
end
In other words, I want the output to be the function of the curve. Hope I am making sense. Thank you for all the comments/ replies.
Hi,
I would suggest looking into that book
Piegl, Les and Tiller, Wayne. The NURBS book (2nd ed.) page 70
You can implement this pretty smart without recursion (assuming closed B-Splines)
function [N] = basisfuns(p,i,u,U)
%returns functionsvalues of basisfunctions in point u
%p degree, i index,u value, U knotvector
%Piegl, Les and Tiller, Wayne. The NURBS book (2nd ed.)
%page 70
N = zeros(1,p+1);
left = zeros(1,p);
right = zeros(1,p);
N(1) = 1;
for j=1:p
left(j) = u - U(i-j+1);
right(j) = U(i+j) - u;
saved = 0;
for r = 0:j-1
temp = N(r+1)/(right(r+1) + left(j-r));
N(r+1) = saved+right(r+1)*temp;
saved = left(j-r)*temp;
end
N(j+1) = saved;
end
Also in the case you need an algorithm for finding the index i:
function [i] = findspan(p,U,u)
%return index i for knotspan, w.r.t. value u
%p degree, U knotvector, u value
%Piegl, Les and Tiller, Wayne. The NURBS book (2nd ed.)
%page 68
n = size(U,2) - (p+1);
if u == U(n+1)
i = n;
return
end
low = p;
high = n+1;
mid = ceil((low + high) / 2);
while u < U(mid) || u >= U(mid+1)
if u < U(mid)
high = mid;
else
low = mid;
end
mid = ceil((low + high) /2);
end
i = mid;
end
And if you want the value on the curve and not only the values of the basis functions do this:
function [C] = curvepoint(p,U,P,u)
%returns functionvalue C on b-spline curve
%p degree, U knotvector, P controlpoints, u value
%Piegl, Les and Tiller, Wayne. The NURBS book (2nd ed.)
%page 82
span = findspan(p,U,u);
N = basisfuns(p,span,u,U);
C = zeros(1,size(P,2));
for i = 0:p
C = C + N(i+1)*P(span-p+i,:);
end
end
And a lot more very nice and pretty fast algorithms (all in C, so you need to "translate" them to MATLAB) can be found there. Also for surfaces and volumes. Take a look its worth it ;)
CS
2020년 4월 8일
in your "basisfuns(p,i,u,U)', when it gets to the line "left(j) = u - U(i-j+1)", MATLAB gives the error "Array indices must be positive integers or logical values". Why? How to solve this?
Hossein Sehhat what values are you passing to curvepoint ?
I didn't get to the curvepoint yet.
I want to give the values for u as
u=0:5
It gives the error "Unable to perform assignment because the left and right sides have a different number of elements.".
I want to draw on the same chart the 2 nd, 3rd, and 4th degree B-spline curves for the seven control points. Thus, the value for p would be 2, 3, and 4, for the 2nd, 3rd, and 4th degrees. Also, the value of u should be continous to be able to plot the curve. Considering u=0:5, MATLAB gave me the above error (as can be seen in the above functions, the value of u is specified at only one point.).
How should I solve these and plot the curves?
Appreciate your help!
The code is only designed for scalar u, and would require significant rewrites for nonscalar u.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Splines에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
