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.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 17일

0 개 추천

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

Joon Jeon
Joon Jeon 2012년 3월 17일
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.
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 2012년 3월 17일
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 2012년 3월 19일
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
What is the difficulty in applying exp() to a negative value?
Joon Jeon
Joon Jeon 2012년 3월 30일
It's from the rule of exponential.
X in the log(x) must be positive value when transform to exponential one.
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by