Problems using solve and syms when summing over vector: Error using mupadengine/feval

I am trying to solve for a parameter that will allow me to discover the "width" that I need to go around the number Y in the vector "Vector" to find exactly 100 vector elements. Specifically, the way that it's described in the code is that in order to be within the range, items in Vector must be within X% of Y. For example, one example of a way to use this would be to find how far out I need to go in percentage terms to find the 100 students whose scores fall closest to 80% (either above or below). (I realize the way I'm doing it right now seems convoluted, but it has to do with other parts of my program).
However, I can't seem to get the whole syms/solve thing to work. Here is my code:
syms X;
width= solve(100 == sum(((Y- X*Vector(:) ) <= Vector(:)) & (Vector(:) < (Y+ X*Vector(:) ))),X);
When I run this I get the errors:
Error using mupadengine/feval (line 163)
Invalid argument.
Error in solve (line 294)
sol = eng.feval('solve', eqns, vars, solveOptions);
Error in myprogram (line 421)
width = solve(100 == sum(((Y- X*Vector(:) ) <= Vector(:)) & (Vector(:) < (Y+
X*Vector(:) ))),X);
The same backbone of this code works if instead of solving for X, I specify it ahead of time and then allow the number of observations within the range to vary.
I have not worked with symbolic variables and the solve command at all in the past, so please forgive me if this question seems basic. I'm not sure if the inequalities are throwing things off or the fact that I'm summing over a vector.
Any help would be appreciated!

Respuestas (2)

Symbolic comparisons do not convert into numeric values and cannot be sum() without going through a lookup function that does the conversion.
Also, which MATLAB version are you using? Some of the handling of inequalities are relatively new.

4 comentarios

I'm using Matlab R2015a.
What do you mean by a lookup function? Is this something that would be very time/processor-intensive on the part of Matlab? I'm doing this as part of a loop where I'll solve for this value for tens of thousands of points in the vector and I don't want Matlab to blow up.
The basic conversion is
TF2N = @(V) piecewise(V,1,0)
You might perhaps be able to use
width= solve(100 == sum(piecewise( ((Y- X*Vector(:) ) <= Vector(:)) & (Vector(:) < (Y+ X*Vector(:) ), 1, 0))),X);
but it would not surprise me if I got the () at the wrong positions
Hmm, it looks like piecewise is not available in version R2015a so I will need to figure something else out. Thanks!
function result = TF2N(V)
result = false(size(V));
for K = 1 : numel(V)
result(K) = isAlways(V, 'Unknown', 'error');
end

Iniciar sesión para comentar.

I don't think you need to use solve to solve this problem. Since this sounds like it may be a homework assignment I want to give only a hint, but it's hard to give a hint that doesn't just solve the problem. So I'm just going to point you to a pair of functions that will help: abs and sort.

Etiquetas

Preguntada:

el 2 de Mzo. de 2018

Comentada:

el 3 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by