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!

 채택된 답변

Friedrich
Friedrich 2012년 8월 28일
편집: Friedrich 2012년 8월 28일

2 개 추천

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

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
Walter Roberson 2012년 8월 28일
Do you mean that you are writing a recursive function ?
jljl
jljl 2012년 8월 28일
Yes I am writing a recursive function.
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.
Friedrich
Friedrich 2012년 8월 29일
편집: Friedrich 2012년 8월 29일
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
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?
Walter Roberson
Walter Roberson 2020년 4월 8일
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!
Walter Roberson
Walter Roberson 2020년 4월 9일
The code is only designed for scalar u, and would require significant rewrites for nonscalar u.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Splines에 대해 자세히 알아보기

질문:

2012년 8월 28일

댓글:

2020년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by