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

Respuestas (1)

madhan ravi
madhan ravi el 2 de Abr. de 2019

0 votos

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

6 comentarios

saeed rezaei
saeed rezaei el 2 de Abr. de 2019
Editada: saeed rezaei el 2 de Abr. de 2019
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 ?
madhan ravi
madhan ravi el 2 de Abr. de 2019
saeed?? Upload the function.
saeed rezaei
saeed rezaei el 2 de Abr. de 2019
saeed is a desirable name. its my name :)
function [out]=saeed(eq,n)
clc
clear all
eqn=eq(n)
end
madhan ravi
madhan ravi el 2 de Abr. de 2019
Editada: madhan ravi el 2 de Abr. de 2019
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.
saeed rezaei
saeed rezaei el 2 de Abr. de 2019
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
madhan ravi
madhan ravi el 2 de Abr. de 2019
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

Iniciar sesión para comentar.

Categorías

Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2015a

Preguntada:

el 2 de Abr. de 2019

Comentada:

el 2 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by