how to make the inline function can identify the matab coded function

1 次查看(过去 30 天)
I am using the sybolic toolbox of matlab but find the inline function cant identify the matlab coded function. For example,
if i use
syms x y
FT=@(x, y)min(x, y);
ans=dblquad(FT, -1, 1, -1, 1);
the answer is correct.
but if i use it alternatively as
syms x y
FT=inline(min(x, y));
ans=dblquad(FT, -1, 1, -1, 1);
it is failed.
So how to make the inline function to identify the matlab's own function?
Thanks so much

采纳的回答

Walter Roberson
Walter Roberson 2012-1-31
syms is irrelevant to what you are doing. You should not be using syms.
Your first approach passes a function handle of two arguments as the first argument to dblquad(). dlbquad() will pass the function a vector for the first argument, and a scalar for the second argument. min() is happy to work with that, and will compare each value in x to the scalar in y.
Your second approach tries to evaluate min(x,y) and pass the result to inline(). In your code, because you defined x and y as syms, min(x,y) is going to be a symbolic expression. inline() requires, however, that it be passed a character expression. Correct coding would be to leave out the syms command and use
FT = inline('min(x,y)');
By the way, your first code version, with the function handle FT, could have been coded as simply
ans = dblquad(@min, -1, 1, -1, 1);
  12 个评论
Walter Roberson
Walter Roberson 2012-2-3
Ah, the silly routine produces a DOM_poly object that has to be converted to an expression.
Try:
syms v w
for K = 1 : N1
tlpg = feval(symengine, 'orthpoly::legendre', sym(K), w);
lgp{K} = matlabFunction( tlpg(v), v);
end
And if that doesn't work, the more verbose
syms v w
for K = 1 : N1
tlpg = feval(symengine, 'orthpoly::legendre', sym(K), w);
lgp{K} = matlabFunction( feval(symengine, 'evalp', tlpg, sym('w=v') ), v);
end
The legendre polynomials do not normally have two variables. Are you just looking for consistency in invocation, looking ahead to a time when you might be using a function with more than one variable? If so then,
for i = 1 : N1
fx = @(x,y) lgp{i}(x);
for j = 1 : N1
fy = @(x,y) lgp{j}(y);
Ans(i,j) = dlbquad(@(x,y) min(x,y).*fx(x,y).*fy(x,y),a, b, a, b);
end
end
Note: I do not have the symbolic toolbox, so the evalp code has not been tested. (I use Maple, which handles the legendre polynomials differently.)

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by