Global symbolic variable definition

조회 수: 25 (최근 30일)
Kazim
Kazim 2013년 3월 1일
댓글: Walter Roberson 2016년 12월 17일
Hi Dear Matlab users
I defined a variable in .M file and assigned a symbolic value to that variable.Then I defined a function and I want to use that variable in this function I wrote.
I tried to define that variable global but program declares it double default and when it tries to assign the symbolic equation to that variable; it can not convert sym to double. How can I solve this problem ?
Thank you
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 1일
How did you know that program declares it double?
Kazim
Kazim 2013년 3월 1일
편집: Kazim 2013년 3월 1일
I know it makes the variable double because gives me an error that can not convert sym to double when the program tries to assign the symbolic expression to that variable. For example;
Global a
syms x y
a=x^2*y ----> Here it says can not convert sym to double . I just want my variable a becomes global not double. I want the variable remains symbolic. How could it be achieved ?
Global syms a ----> If I write like that syntax error it says

댓글을 달려면 로그인하십시오.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 1일
편집: Azzi Abdelmalek 2013년 3월 1일
In windows command you should declare a as a sym global
syms a
global a
  댓글 수: 3
mohammad m
mohammad m 2016년 12월 17일
편집: mohammad m 2016년 12월 17일
hi in matlab 2013 i use like this but i get error that : undefined variable's. i want to use
syms x
global x
in my code, Then in my function in other mfile use this variable without syms in funcyion, like this
function a=f
f=x
end
but i get error, its not possible or my code is wrong? i don't want to use syms in my function, because it reduce speed.
Walter Roberson
Walter Roberson 2016년 12월 17일
global x %has to go before the syms
syms x
and then in your function
function a=f
global x
a=x
end
If you are trying to avoid the syms call because of the overhead, but want to use the symbol, then you should consider passing it to your function instead of using global. global is the slowest method of accessing variables. You should consider parameterizing your functions to include the symbol that you want to work with.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Variables, Expressions, Functions, and Preferences에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by