How can I make MatLab output unassigned variables?
조회 수: 7 (최근 30일)
이전 댓글 표시
I'm used to Mathematica, so I'm not sure how and if the same works with MatLab. Is there a way to input something like:
x = 3t
and have x be equal to 3t until you assign t a value?
edit:
so I have this code:
function [zr,w,R,q,mat] = test(z)
lambda = 1;
w0 = 1;
zr = (pi*w0^2)/lambda;
w = w0*sqrt(1+(z/zr)^2);
R = z + zr^2/z;
q = z + 1i*zr;
mat = [1 z; 0 1];
end
but I not want to have to assign lambda and w0, i want them to just stay lambda and w0 UNLESS I assign them ... so inputting
[zr,w,R,q,mat] = test(z)
should return the variables exactly how they look inside the function:
zr =
(pi*w0^2)/lambda
w =
w0*sqrt(1+(z/zr)^2)
R =
z + zr^2/z
q =
z + 1i*zr
mat =
1 z
0 1
also, how come these don't exist outside of the function? I can't call q anymore for example after executing test because it doesn't appear to exist anymore
댓글 수: 1
Stephen23
2015년 5월 15일
편집: Stephen23
2015년 5월 18일
There is a deep and fundamental difference between numeric computations (e.g. MATLAB's core functionality), and symbolic computations (e.g. Mathematica's core functionality). These differences determine how and why things have to be done, and one important part of numeric calculations is that they require actual numeric values to be defined before any calculations. If you wish to avoid already having values and only want relationships and mappings, then you can create functions, as Chad Greene explains below, or the Symbolic Math Toolbox as Star Strider explains below.
채택된 답변
Star Strider
2015년 5월 15일
If I remember correctly, Mathematica is primarily a symbolic language. The most direct way to emulate Mathematica in MATLAB is to use the MATLAB Symbolic Math Toolbox.
For example:
syms x(t)
x(t) = 3*t;
xt = x(t)
t = 9;
xt = x(t)
produces:
xt =
3*t
xt =
27
댓글 수: 4
Star Strider
2015년 5월 17일
You have to use the function version (I believe this was introduced in R2012a))?
syms x(t)
x(t) = 3*t;
That, and my original code, worked as I posted it and gave the correct output. If it doesn’t work, you will likely have to use the subs function.
Walter Roberson
2015년 5월 17일
I do not recommend assigning a numeric value to a symbol name to try to have the former symbol interpreted as the current number. You can do it, by using
subs(x)
which will look through x for all symbols and check to see if those symbols have current numeric values and if so then substituting the numeric value in. When you get to a symbolic result that you are sure has no remaining symbols, you can double() the symbolic number to get it into floating point.
I recommend explicit substitutions without assigning to the symbol name:
tval = 2;
subs(x,t,tval)
Again if you know that no symbols will remain in the expression, you can double() the result
double(subs(x,t,tval))
You don't have to use an intermediate variable; I just wanted to make clear where the value was coming from.
double(subs(x,t,2))
is fine if 2 is the value you want to substitute for t.
추가 답변 (1개)
Chad Greene
2015년 5월 15일
편집: Chad Greene
2015년 5월 15일
You can do this with an anonymous function. Here's how:
myfun = @(t) 3*t;
Then
myfun(4)
= 12
or
x = myfun(2)
x = 6
and so on. When myfun is defined, I've said that myfun is a function of t, and that the output of myfun(t) will always be 3*t.
댓글 수: 1
Walter Roberson
2015년 5월 18일
The original poster would like to be able to see the formulae in terms of what is already known, so that if a value is assigned to a variable, the formula simplifies down based upon the constant values. You can't do that with anonymous functions alone, as you always have to call an anonymous function on some argument, and if the argument is not defined then you have a problem. You can work with a combination of anonymous functions and symbolic variables, but if you are going to do that then you might as well skip the anonymous function level.
참고 항목
카테고리
Help Center 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!