How can I make MatLab output unassigned variables?
Mostra commenti meno recenti
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 Commento
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.
Risposta accettata
Più risposte (1)
Chad Greene
il 15 Mag 2015
Modificato: Chad Greene
il 15 Mag 2015
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 Commento
Walter Roberson
il 18 Mag 2015
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.
Categorie
Scopri di più su Symbolic Math Toolbox in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!