vpasolve - "The following error occurred converting from sym to double: DOUBLE cannot convert the input expression into a double array."

6 views (last 30 days)
Hello,
we use Chebyshev Polynomials to approximate a function (for our problem, it is not necessary to know the approximation by Chebyshev Polynomials). The approximation works well. Now we search for a specific y-value the respective x-value (which equals U(1)). For that we tried to use vpasolve. Our approach is working (see code below), but it takes a really long time. We guess it has something to do with the conversion from double to sym. We tried to convert everything to doubles, but then we received the following error message for the following line:
T(2, :) = z;
"The following error occurred converting from sym to double: DOUBLE cannot convert the input expression into a double array." We tried to convert all variables to double, but somehow it went wrong.
Does anybody know how to do the conversion from sym to double right in this case? Or is there in general a better way how we could solve our problem of getting the x-value for a specific y-value?
Thank you very much for any advice!!
This is the extract of the code which is running so far, but needs a very long time.
syms g
test = vpasolve(U(1) - chebEval1(coeffs, g, minWage,maxWage) == 0,g);
function val = chebEval1(coeffs,x,xMin,xMax)
% evaluate approximated function at x
% coeffs: column vector with coefficients
%
% inputs:
% coeffs ((n+1)x1 array)
% x (scalar) contains the x value at which the Chebyshev polynomial
% is evaluated.
% xMin (scalar) is the lower bound of the intervall (i.e. a in
% [a,b])
% xMax (scalar) is the upper bound of the intervall (i.e. b in
% [a,b])
%
% output:
% val (scalar) the y value.
N = size(coeffs, 1); % number of coefficients
z = (x-xMin).*2./(xMax-xMin) - 1; % z in range [-1, 1]
T = sym(zeros(N, 1));
T(1, :) = 1;
T(2, :) = z;
for i = 3:N
T(i, :) = 2.*z'.*T(i-1, :) - T(i-2, :);
end
val = vpa(T'*coeffs);
end
  2 Comments
Walter Roberson
Walter Roberson on 22 Jan 2020
That code cannot produce the error message you indicate. However, if you had
T = zeros(N, 1);
in the version you tested, then that code could potentially produce that error, if x or xMin or xMax were symbolic.
Nora Barschkett
Nora Barschkett on 23 Jan 2020
Thanks for the fast response. Yes we had before T = zeros(N,1), and then we received the error message we indicated in our text above. To solve the error message we changed the line into
T = sym(zeros(N, 1));
With this line, our code workes fine. However, by using T = sym(zeros(N, 1)) it takes a high computational time to run the code. We guess the high computational time results from using sym instead of doubles, but we are not sure if it is really because of that. Do you maybe have an idea on how to make the computational time shorter? Is there a way to declare x, xMin, xMax as a double insteadt of a symbolic and then using the line T = zeros(N,1)?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!