Question about the solve function

3 visualizzazioni (ultimi 30 giorni)
Peter
Peter il 21 Mag 2012
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

Risposta accettata

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

Più risposte (2)

Walter Roberson
Walter Roberson il 21 Mag 2012
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 Commenti
Sean de Wolski
Sean de Wolski il 21 Mag 2012
Error using double
Conversion to double from struct is not possible.
Sean de Wolski
Sean de Wolski il 21 Mag 2012
S = solve(e1,e2)
double(S.b)
double(S.c)
@Peter: Walter's method is the better way to do it!

Accedi per commentare.


Peter
Peter il 21 Mag 2012
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 Commento
Walter Roberson
Walter Roberson il 21 Mag 2012
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

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by