Using latex command with anonymous functions

1 回表示 (過去 30 日間)
Leo Simon
Leo Simon 2012 年 8 月 13 日
I want to convert a bunch of simple symbolic expressions to latex
e.g.,
syms x;
B = @(x)B(x)
latex(diff(B,x)) produces latex output
but
latex(B(x)) produces an error.
Of course, if I had
B = sym('B(x)')
latex(B(x))
would do what I want it to do, i.e., output B(x) but then
latex(B(y))
would produce an error

回答 (6 件)

Alexander
Alexander 2012 年 8 月 14 日
In R2012a I can do this:
>> syms x y B(x)
>> latex(B(x))
ans =
B\!\left(x\right)
>> latex(B(y))
ans =
B\!\left(y\right)
>> latex(diff(B(x)))
ans =
\frac{\partial}{\partial x} B\!\left(x\right)
  1 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 8 月 14 日
編集済み: Oleg Komarov 2012 年 8 月 15 日
I like this approach.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2012 年 8 月 13 日
Perhaps
latex(char(B(x)))

Matt Fig
Matt Fig 2012 年 8 月 13 日
B = sym('B(x)');
latex(B)
latex(subs(B,'x','y'))

Leo Simon
Leo Simon 2012 年 8 月 13 日
Thanks to Matt and Walter for responses. Walter's answer didn't work; according to the documentation, latex requires a symbolic argument. Matt's answer did work, but I'd really like something more general if somebody has one, specifically, I'd much prefer to be able to define B as an anonymous function of the symbolic variable x, and then latex the output, e.g., i'd like to be able to write
syms x;
B(x) = @(x)B(x)
and have
latex(B(x))
return
B\left(x\right)
Obviously this doesn't happen, but I'm hoping that some modification of my proposed command would work.
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 8 月 13 日
latex( sym(char(B(x))) )

サインインしてコメントする。


Oleg Komarov
Oleg Komarov 2012 年 8 月 13 日
Interesting case, do you define B in a recursive way on purpose?
However the error is clear, B doesn't exist at the moment of the call B(x).
To make it more clear:
syms x
f = @(input) B(input);
f(x)
Undefined function 'B' for input arguments of type 'sym'.
Error in @(input)B(input)

Leo Simon
Leo Simon 2012 年 8 月 13 日
Well, I've tried defining B as a symbolic variable first, but that doesn't help, e.g.
clear all ; syms x B ; f= @(input)B(input) ; f(x)
Which just generates the infuriating, opaque, ubiquitous message
??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
So I'm not sure how to make B exist any other way?
  2 件のコメント
Matt Fig
Matt Fig 2012 年 8 月 13 日
Leo, please comment on the answer you are engaging. Don't just keep adding 'answers' that aren't really answers to the OP. Thanks.
Leo Simon
Leo Simon 2012 年 8 月 14 日
Ok, sorry

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by