transform to input to symbol

Dear all
how can i creat a function which take any desired variable like x or y or z and transform it to a symbol. i mean the function take the x and give back to symbol x.
thanks alot

回答(1 个)

syms(sym('x')) % works only for scalar variables , bearing in mind this is only used for symbolic calculations.

6 个评论

thanks for your helping
let me explain my problem exactly with another example.
suppose that we created a function ( saeed function) which takes an expression and a number. then calculates the expression based on the number and gives back the result. for example:
>>saeed(x^2+x-5,2)
ans:
1
the function is like this
function [out]=saeed(eq,n)
the problem is that when user enter this : saeed(x^2+x-5,2) the matlab gives an error. the error is:
Undefined function or variable 'x'.
as you know we can define x as a symbol. but we can do this befor function. now, what should i do ?
saeed?? Upload the function.
saeed is a desirable name. its my name :)
function [out]=saeed(eq,n)
clc
clear all
eqn=eq(n)
end
eq=input('Enter the equation:','s') % Type x^2+x-5
n=2;
eqn = str2func(['@(x)',eq]);
eqn(n)
Note: Remove clc and clear all inside your function.
yeah
i know this way. but this code is not a function.
i wanna write the code as a function so that the user can enter saeed(eq,n) and take back the answer
eq=input('Enter the equation:','s') % Type x^2+x-5
n=2;
Result = saeed(eq,n)
function Result=saeed(eq,n)
eqn = str2func(['@(x)',eq]);
Result=eqn(n);
end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品

版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by