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."

采纳的回答

Walter Roberson
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.
  1 个评论
Sandeep Parameshwara
Thanks a lot, this is excatly what I needed. Now onwards, I will use the interpolation functions like you demonstreted.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by