2

Imagine I define two variables within a MuPad Notebook:

x:=2;
y:=5

For the product

z=x*y

I get displayed:

enter image description here

And if I use hold, I can get the expression:

z=hold(x*y)

enter image description here

But now I'd like to have both, the expression displayed and the result. The two options which appeared logical to me, do not worK:

z=hold(x*y);z

and

z=hold(x*y);eval(z);

enter image description here

How can I get displayed the expression AND the result? If in two lines it would be alright, but I'd prefer in one line like:

z = x y = 10

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • What about `val(z)` ? – Ander Biguri Jul 24 '15 at 10:43
  • It does not seems to make a difference to `eval(z)`... – Robert Seifert Jul 24 '15 at 10:57
  • The problem is that you don't assign `x*y` to `z` in the code above because you left out the colon `:`. You can use `z:=hold(x*y);z`. – Matt Jul 24 '15 at 15:54
  • @Matt thats the code I already tried? – Robert Seifert Jul 24 '15 at 15:56
  • 1
    In your code, the colon is missing. You have `z=` instead of `z:=`. Therefore you never assign something to `z`. Since `z` is undefined it will be always `z` without a value. – Matt Jul 24 '15 at 15:57
  • @Matt I understand, I finally got the difference between `:=` and `=` - do you see a possibilty to get the desired output? Some tweak or macro? – Robert Seifert Jul 24 '15 at 16:00
  • I tried a bit, but couldn't find a convincing solution now. You can try using a combination with [`print`](http://ch.mathworks.com/help/symbolic/mupad_ref/print.html), [`expr2text`](http://ch.mathworks.com/help/symbolic/mupad_ref/expr2text.html), `hold` and `_concat`. When you have the expected behaviour, there is the possibility to write a procedure. In the documentation for `expr2text` is an example. – Matt Jul 24 '15 at 16:35
  • @Matt I also fiddled around a little, seems it's not possible to get everything as convenient as in MathCad. However If you'd write an answer explaining the differences between `:=` and `=` and that this was the problem, I'll accept it. If you don't want to, let me know and I'll do to get this thing solved. – Robert Seifert Jul 24 '15 at 17:06
  • Thanks, I'll write this answer tomorrow. – Matt Jul 24 '15 at 21:10

1 Answers1

2

I tried some combinations with print, expr2text, hold and _concat but couldn't find a convincing solution to get the desired result. But there is an explanation why the second line just returns z and not 10.

Assignment vs. Equation

z is the result in the second line because you didn't assign something to z yet. So the result says that z is z. In MuPad = is part of an expression. The assignment operator is := and therefore not the same as in Matlab. The only difference between them is the colon.

Writing an equation

For writing an equation, we use = as part of the expression. There is an equivalent function: _equal. So the following two lines generate the same result:

x+y = 2
_equal(x+y, 2)

result1

Assign value to x

For an assignment we use := (in Matlab this would be only =). There is an equivalent function: _assign. So again, the following two lines generate the same result:

x := value
_assign(x, value)

result2

Assign the equation x+y = 2 to eqn

Here we can clearly see the difference:

eqn := x+y = 2
_assign(eqn, _equal(x+y, 2))

result3

Community
  • 1
  • 1
Matt
  • 12,848
  • 2
  • 31
  • 53