필터 지우기
필터 지우기

Accessing function output for different values of variable

조회 수: 2 (최근 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
Sandeep Parameshwara 2019년 12월 22일
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