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

Réponses (1)

madhan ravi
madhan ravi le 2 Avr 2019

0 votes

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

6 commentaires

saeed rezaei
saeed rezaei le 2 Avr 2019
Modifié(e) : saeed rezaei le 2 Avr 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 le 2 Avr 2019
saeed?? Upload the function.
saeed rezaei
saeed rezaei le 2 Avr 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 le 2 Avr 2019
Modifié(e) : madhan ravi le 2 Avr 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 le 2 Avr 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
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by