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 comentario

Stephen23
Stephen23 el 15 de Mayo de 2015
Editada: Stephen23 el 18 de Mayo de 2015
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 15 de Mayo de 2015
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 comentarios

More direct would be
syms x
x = 3 * t;
and then later when you assign t a value,
subs(x)
would make the connection between the symbol t and the numeric value assigned to t.
I do not recommend that form of subs() though: I recommend
subs(x, t, TheNumericValue)
Solarmew
Solarmew el 17 de Mayo de 2015
Editada: Solarmew el 17 de Mayo de 2015
for some reason I still got xt = 3*t after inputting t=9;
and by writing:
syms t
x = 3*t;
x
outputs x = 3*t . But then how do I assign t a value and have x change accordingly? I did
t = 2;
x
but it still returned 3*t
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.
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.

Iniciar sesión para comentar.

Más respuestas (1)

Chad Greene
Chad Greene el 15 de Mayo de 2015
Editada: Chad Greene el 15 de Mayo de 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 comentario

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.

Iniciar sesión para comentar.

Categorías

Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Mayo de 2015

Comentada:

el 18 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by