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개)

madhan ravi
madhan ravi 2019년 4월 2일

0 개 추천

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

댓글 수: 6

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

질문:

2019년 4월 2일

댓글:

2019년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by