why does my code take so long to run?
Ältere Kommentare anzeigen
I am trying to compute v' * f(A) * v, where f is a given function, A matrix, and v is a unite vector.
My code takes along time doing run and did not get the result. Could please help me to fix this problem?
% want to compute v'*f(A)*v
syms f(x)
N=1000;
Ns=1:N;
R=1./(Ns);
A=sym(toeplitz(R));% the input matrix
v=ones(N,1);
v=v/norm(v);
f(x) = atan(sqrt(x)); % the function f(x)=arctan(sqrt(x))/x
B = funm(A,f); % the resulting matrix
y=A\v;
exact=v'*B*y
5 Kommentare
Walter Roberson
am 8 Okt. 2020
Original code:
% want to compute v'*f(A)*v
syms f(x)
N=1000;
Ns=1:N;
R=1./(Ns);
A=sym(toeplitz(R));% the input matrix
v=ones(N,1);
v=v/norm(v);
f(x) = atan(sqrt(x)); % the function f(x)=arctan(sqrt(x))/x
B = funm(A,f); % the resulting matrix
size(A)
size(v)
Rena Berman
am 9 Okt. 2020
(Answers Dev) Restored edit
Walter Roberson
am 10 Okt. 2020
Omar, I had my computer working on your question for several hours, gave it about an hour of my attention. It would not be fair to me to delete the question after I took the time to respond.
Rik
am 10 Okt. 2020
Bearbeitet: Rena Berman
am 12 Okt. 2020
@Omar, Please explain why you want your question removed.
Rena Berman
am 12 Okt. 2020
(Answers Dev) Restored edit
Antworten (1)
Hiro Yoshino
am 8 Okt. 2020
0 Stimmen
It looks that your problem is not that complicated. Why don't you write your code without using symbolic expressions? I bet it is faster.
3 Kommentare
Omar B.
am 8 Okt. 2020
Walter Roberson
am 8 Okt. 2020
There is a challenge that the numeric funm is different than the symbolic funm .
For the symbolic funm, you pass in a symbolic function or symbolic expression, and funm automatically takes derivatives in order to build a taylor series (or however it is represented). No control is offered over the number of terms used, and all the derivatives are likely to be exact derivatives.
If we look at even just the sqrt() part, as a matrix function, sqrtm(), that mathematically involves a closed form svd, sqrt of the diagonal, and reconstruct -- all as closed form expressions. For a 1000 x 1000 matrix, that is going to take a very long time.
For the numeric funm, you pass in a function handle to a function that must accept a vector and must also accept a derivative number to calculate and evaluate the input at. But you do not have diff() available to help because it is a numeric function handle you are working on, not a symbolic function. So you need to have a formula to calculate the N'th derivative.... Or you need to slip into the symbolic world, probably memoizing as you go.
Walter Roberson
am 8 Okt. 2020
Even for N=10 the symbolic version takes much much much too long.
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!