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
0 件のコメント
採用された回答
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
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!