Creating a symbolic variable, then using it as a real variable

1 回表示 (過去 30 日間)
Tom
Tom 2012 年 10 月 25 日
I need my program to calculate the derivative of a function, and as far as I understand that means I need to make a symbolic variable. How then can I continue to use that variable as a real vector?
This is what I have tried:
syms x
y = input('Please enter a valid function of x')
deriv = diff(y)
syms clear
x = 0:pi/30:pi;
plot(x,y)

採用された回答

Matt Fig
Matt Fig 2012 年 10 月 25 日
編集済み: Matt Fig 2012 年 10 月 25 日
syms x
y = input('Please enter a valid function of x: ')
deriv = diff(y) % This will be symbolic
x = 0:pi/30:pi; % Now x is a double.
y = matlabFunction(y) % y is a function handle.
plot(x,y(x))
  2 件のコメント
Tom
Tom 2012 年 10 月 25 日
Thanks a lot, Matt! Can I ask, why does that only work with y(x) in the plot argument, instead of just y?
Matt Fig
Matt Fig 2012 年 10 月 25 日
編集済み: Matt Fig 2012 年 10 月 25 日
y is a function handle (as I say in the comment!), not a vector. If you want to make y a vector instead:
syms x
y = input('Please enter a valid function of x: ')
x = 0:pi/30:pi; % Now x is a double.
y = subs(y); % y is now a double.
plot(x,y)
Or without the symbolic toolbox at all:
x = 0:pi/30:pi;
y = 'Please enter a valid function of x: ';
y = feval(vectorize(inline(input(y,'s'))),x);
plot(x,y)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 10 月 25 日
Use matlabFunction()
  2 件のコメント
Tom
Tom 2012 年 10 月 25 日
編集済み: Tom 2012 年 10 月 25 日
I couldn't work out how to use that. Can you see why this doesn't work?
syms x
y = sin(x)
deriv = diff(y)
subs(y,[x],[0:pi/30:pi])
plot(x,y)
Tom
Tom 2012 年 10 月 25 日
Or indeed...
syms x
fx = input(':Please enter a valid function of x:')
dydx = diff(fx)
y = matlabFunction(fx)
plot(x,y)

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

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by