How can I make MatLab output unassigned variables?
이전 댓글 표시
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
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.
채택된 답변
추가 답변 (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.
카테고리
도움말 센터 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!