Simple question. Using 'solve' with vectors

Hi, I am a matlab rookie. Since I am doing simulation work recently, I start to learn matlab from A, again.
Here is my question. I want to deliver 'x' from the following equation such as log(x)=2/(A-B)
Here, A and B are [10,1] vectors with numbers.
So, my result x should get some matrix like x[10,1].
For this, I programmed like,
CC=zeros(10,1);
for i=1:10
syms x
solve(log(x(i))-(A(i)-B(i))/0.67)
CC(i)=CC(i)+x
end
but the command window keeps saying "??? The following error occurred converting from sym to double: Error using ==> mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead."
1. What should I change?
2. If I obtain the result, matlab usually shows in expoenetial terms. How can I order matlab to calculate completely?
Thanks.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Mzo. de 2012

0 votos

solve() is not going to store the solutions into "x": it is only going to return the value (which would then be displayed in the command window.)
Because of that, when you get to the CC(i) = CC(i) + x line, x is still a basic symbolic variable whose "value" is its own name, but CC is double precision, so MATLAB tries to convert the name x to a double precision value and of course fails.
Assign the result of solve() to something, and add that something to CC(i) .
You may also need to use a call to double() to force conversion from symbolic numbers to double precision.

7 comentarios

Joon Jeon
Joon Jeon el 17 de Mzo. de 2012
Thanks for quick response.
If you don't mind, let me raise one more question. I tried what you advised such as followings.
Z=[]
for i=1:22
syms x
solve (log(x(i))-(EU(i)-US(i))/0.67)
Z(i)=Z(i)+double(x)
end
However, now it says "Index exceeds matrix dimensions".
Then, I REMOVED all the (i)s in solve function such as
for i=1:22
syms x
solve (log(x)-(EU(i)-US(i))/0.67)
end
And now it gives answers(but not matrix, you know).
So, I got back to the original and removed Z(i) terms.
Then, it doesn't work.
Hm, doesn't solve save its result in x? Then, is there any other commands you can recommend to me? I need the result in matrix form.
Thanks in advance.
Walter Roberson
Walter Roberson el 17 de Mzo. de 2012
As I said before "Assign the result of solve() to something, and add that something to CC(i) ."
syms x
for i = 1 : 22
xval = solve(log(x)-(EU(i)-US(i))/0.67);
Z(i) = Z(i) + double(xval);
end
Or, more simply, just use
Z = exp((EU - US)/0.67);
to replace all of that code.
Joon Jeon
Joon Jeon el 17 de Mzo. de 2012
Walter, it's not.
It keeps saying "Index exceeds matrix dimensions"
And since (EU-US) is sometimes negative, I cannot apply logarithm rule like that.
Alexander
Alexander el 19 de Mzo. de 2012
Are you sure your variables EU, US, and Z are big enough? This works for me:
EU = rand(22, 1);
US = rand(22, 1);
Z = zeros(22, 1);
syms x
for i = 1 : 22
xval = solve(log(x)-(EU(i)-US(i))/0.67);
Z(i) = Z(i) + double(xval);
end
Walter Roberson
Walter Roberson el 19 de Mzo. de 2012
What is the difficulty in applying exp() to a negative value?
Joon Jeon
Joon Jeon el 30 de Mzo. de 2012
It's from the rule of exponential.
X in the log(x) must be positive value when transform to exponential one.
Walter Roberson
Walter Roberson el 30 de Mzo. de 2012
If log(x)-(EU(i)-US(i))/0.67 = 0 (condition to be solved) then
log(x) = (EU(i)-US(i))/0.67 and then
exp(log(x)) = exp((EU(i)-US(i))/0.67) so
x = exp((EU(i)-US(i))/0.67)
and at no point are you taking the log of a negative number.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by