Question about the solve function

3 次查看(过去 30 天)
Let's say I have:
A = [ 1 2 ; 3 4 ]
e1='c*A(1,1)^b=A(1,2)'
e2='c*A(2,1)^b=A(2,2)'
I'm trying to use "solve" to find c and b: solve(e1,e2)
The answer looks like: [ log(A(1, 2)/A(2, 2))/(log(A(1, 1)) - log(A(2, 1))), A(2, 2)/A(2, 1)^(log(A(1, 2)/A(2, 2))/(log(A(1, 1)) - log(A(2, 1))))]
I want it to look like: 0.6309 2.0000
Is "solve" the wrong function for this? How do I do to get a numerical answer?
Best Regards Peter

采纳的回答

Sean de Wolski
Sean de Wolski 2012-5-21
One way:
S = solve(e1,e2)
eval(S.b)

更多回答(2 个)

Walter Roberson
Walter Roberson 2012-5-21
syms b c
A = [ 1 2 ; 3 4 ]
e1 = (c*A(1,1)^b) - (A(1,2));
e2 = (c*A(2,1)^b) - (A(2,2));
double(solve(e1, e2))
  2 个评论
Sean de Wolski
Sean de Wolski 2012-5-21
Error using double
Conversion to double from struct is not possible.
Sean de Wolski
Sean de Wolski 2012-5-21
S = solve(e1,e2)
double(S.b)
double(S.c)
@Peter: Walter's method is the better way to do it!

请先登录,再进行评论。


Peter
Peter 2012-5-21
Next question:
A = [ 1 2 ; 3 4 ; 5 6 ; 7 8 ]
When trying to implement it into a loop:
for k= 1:size(A,1)-1
S=solve('c*A(k,1)^b=A(k,2)','c*A(k+1,1)^b=A(k+1,2)');
b=eval(S.b);
C=eval(S.c);
end
it seems like it handles the variable k as an unknown. How do I solve this?
  1 个评论
Walter Roberson
Walter Roberson 2012-5-21
Either don't solve() on quoted strings, or learn to use subs()
syms b c
A = [ 1 2 ; 3 4 ; 5 6 ; 7 8 ]
b = zeros(1,size(A,1)-1);
c = b;
for k= 1:size(A,1)-1
S = solve(c*A(k,1)^b-A(k,2),c*A(k+1,1)^b-A(k+1,2));
b(k) = double(S.b);
C(k) = double(S.c);
end

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by