Accessing function output for different values of variable
1 次查看(过去 30 天)
显示 更早的评论
This question could be very basic (in which case please refer me to relevent guide/tutorial to study), however I am stuck. My code is something like this:
P=@(rho) P0 + P1*sin(rho) + P2*cos(rho);
Q=@(rho) Q0 + Q1*sin(rho) + Q2*cos(rho);
Later, P and Q are solved and co-efficients are stored. Then I use them in
rho_grid= -1:0.1:1
for i=1:length(rho_grid)
rho=rho_grid(i);
Rq= @(rho) chol(value(Q(rho)),'upper');
Rp= @(rho) chol(value(P(rho)),'lower');
pro=@(rho) Rq(rho)*Rp(rho);
[U,S,V] = svd(pro(rho));
end
Please notice that only final values are being stored in U,S and V. I want to access U(rho), V(rho) and S(rho). How can I achieve that?
I tried using something like
[U,S,V] = @(rho) svd(pro(rho));
to which Matlab shows error "Only functions can return multiple values."
0 个评论
采纳的回答
Walter Roberson
2019-12-22
rho_grid= -1:0.1:1;
Nrho = length(rho_grid);
for i = Nrho : -1 : 1 %backwards! For pre-allocation reasons
rho = rho_grid(i);
Rq = @(rho) chol(value(Q(rho)),'upper');
Rp = @(rho) chol(value(P(rho)),'lower');
pro = @(rho) Rq(rho)*Rp(rho);
[Uvals(:,:,i), Svals(:,:,i), Vvals(:,:,i)] = svd(pro(rho));
end
U = @(rho) Uvals(:,:,interp1(rho_grid, 1:Nrho, rho, 'nearest'));
S = @(rho) Svals(:,:,interp1(rho_grid, 1:Nrho, rho, 'nearest'));
V = @(rho) Vvals(:,:,interp1(rho_grid, 1:Nrho, rho, 'nearest'));
Instead of using 'nearest' in computing the index, you could go for something more sophisticated such as determining the mixing between the two closest values, and doing a weighted calculation to interpolate at the rho.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!