Accessing function output for different values of variable

1 view (last 30 days)
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."

Accepted Answer

Walter Roberson
Walter Roberson on 22 Dec 2019
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 Comment
Sandeep Parameshwara
Sandeep Parameshwara on 22 Dec 2019
Thanks a lot, this is excatly what I needed. Now onwards, I will use the interpolation functions like you demonstreted.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!