How do I correctly use vpasolve?

I am trying to numerically solve the following set of equations
M_mag = 1 ; %just some constant simplified here as 1
om = 2*pi*new_freq ; %new_freq is an array of values
C1 = exp(-1i*L.*om./c); % L and c are constant
% phi = exp(1i*om*L/c);
nume = exp(1i.*om.*n*L/c)*4.*n./((1+n).^2); % terms depending on n
denom = 1 + exp(2*1i.*om.*n*L/c).*((n-1)./(n+1)).^2; % terms depending on n
syms n
S = vpasolve(M_mag./C1 == nume./denom , n , guess) %guess is a numerical approach done via another equation, in this case it is 1.7
Whenever I try to use this, I get the following text:
Error using mupadengine/feval_internal
More equations than variables is only supported for polynomial
systems.
Error in sym/vpasolve (line 172)
sol = eng.feval_internal('symobj::vpasolve',eqns,vars,X0);
I have seen similar things being explain in the following link, but I don't understand the explanation. Same for this explanation.
Why can I not solve the equation this way? Is it the exponentials terms?
Thank you very much for your help.

1 comentario

Star Strider
Star Strider el 17 de Nov. de 2022
I usually use solve, and then vpa the result. It is generally more reliable, especially with functions with multiple roots.
Too much of this code is ‘over the horizon’ and out of sight to provide a specific response.

Iniciar sesión para comentar.

Respuestas (2)

Torsten
Torsten el 17 de Nov. de 2022
Editada: Torsten el 17 de Nov. de 2022

0 votos

You have 2 equations for 1 unknown. MATLAB's symbolic toolbox does not solve such systems.
Is n supposed to be real or complex ?

6 comentarios

Goncalo Costa
Goncalo Costa el 17 de Nov. de 2022
But don't I just have one equation, the one inside the vpasolve function? I separated into parts, but overall it is just one equation, one equality, no?
Or am I missing something (I probably am)? Thanks for your help.
Any one call to vpasolve() or solve() is a call to solve simultaneous equations.
syms x
vpasolve( [4*x+5==9, 3*x - 7 == 2] )
does not try to independently solve [4*x+5==0] and [3*x-7==2]: it tries to find a single combination of values of the variables that solves all of the equations at the same time. You would not expect vpasolve( [4*x+5*y==9, 3*x - 7*y == 2] ) to produce independent solutions for each of the equations, and the action of solve() and vpasolve() for multiple equations does not change just because the multiple equations only have a single variable between them.
Your comments say "new_freq is an array of values" so if you work through, M_mag./C1 == nume./denom is an array of equations. A single solve() call would try to find a single x that solves all of the equations at the same time. A single vpasolve() call will simply refuse to handle the situation (except for polynomials)
So you need to ask vpasolve() to solve each of the equations one-by-one . Which is what my arrayfun() Answer does.
Torsten
Torsten el 17 de Nov. de 2022
You'll only get an answer if n can be complex-valued (which I doubt the OP aims at).
Torsten
Torsten el 17 de Nov. de 2022
Editada: Torsten el 17 de Nov. de 2022
Implicitly, you have the equations
real(M_mag./C1 - nume./denom) == 0
and
imag(M_mag./C1 - nume./denom) == 0
And you have a symbolic variable n.
Whether you get a solution that fits your needs depends on whether you accept a complex-valued n or not.
Goncalo Costa
Goncalo Costa el 18 de Nov. de 2022
n is a complex number. Can I change my code for a complex result?
Thank you very much for your help.
Goncalo Costa
Goncalo Costa el 18 de Nov. de 2022
@Walter Roberson that makes sense, I thought that it would solve one-by-one. I didn't understand the limitations of this tool. How can I go around this to have this equation solved for an array? Would a for loop be needed?

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 17 de Nov. de 2022
M_mag = 1 ; %just some constant simplified here as 1
om = 2*pi*new_freq ; %new_freq is an array of values
C1 = exp(-1i*L.*om./c); % L and c are constant
% phi = exp(1i*om*L/c);
syms n
nume = exp(1i.*om.*n*L/c)*4.*n./((1+n).^2); % terms depending on n
denom = 1 + exp(2*1i.*om.*n*L/c).*((n-1)./(n+1)).^2; % terms depending on n
S = arrayfun(@(EQN) vpasolve(EQN,n,guess), M_mag./C1 == nume./denom)

2 comentarios

Goncalo Costa
Goncalo Costa el 18 de Nov. de 2022
Dear @Walter Roberson, I have never understood how arrafun works, but it seems to be giving me the right answer. Have you got any link that explain the function written above?
Thank you for your help.
PS: For some reason, the first cell in the cell array separates the complex from the real part of the number.But that can be easily sorted.
result = arrayfun(FUNCTION, ARRAY)
is effectively the same as
if isempty(ARRAY)
result = [];
else
out1 = FUNCTION(ARRAY(1));
result = zeros(size(ARRAY), class(out1));
result(1) = out1;
for K = 2 : numel(ARRAY)
result(K) = FUNCTION(ARRAY(K));
end
end
and
result = arrayfun(FUNCTION, ARRAY, 'uniform', 0)
is effectively the same as
result = cell(size(ARRAY));
for K = 1 : numel(ARRAY)
result{K} = FUNCTION(ARRAY(K));
end
and
result = arrayfun(FUNCTION, ARRAY1, ARRAY2)
is effectively the same as
assert(isequal(size(ARRAY1), size(ARRAY2)), 'input arrays must be the same size');
if isempty(ARRAY1)
result = [];
else
out1 = FUNCTION(ARRAY1(1), ARRAY2(1));
result = zeros(size(ARRAY1), class(out1));
result(1) = out1;
for K = 2 : numel(ARRAY1)
result(K) = FUNCTION(ARRAY1(K), ARRAY2(K));
end
end
So you input a function handle (typically), and one or more arrays that must be exactly the same size. The output is the same size as the array. When 'uniform', 0 is specified, the output is a cell array containing the result of executing the function on each corresponding sets of values from the array in turn. If 'uniform', 0 is not specified, the output is a regular array containing the result of executing the function on each corresponding set of values from the array in turn.

Iniciar sesión para comentar.

Preguntada:

el 17 de Nov. de 2022

Comentada:

el 18 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by