Obtaining a scalar value from a vector output

Hi, with the given files, if you run test-modified.m and then enzymeLeastSquares.m, you obtain the necessary inputs for running
enzymeGeneral.m
So when running finally enzymeGeneral.m, it outputs F and G.
However, the output from enzymeGeneral.m should be the scalar value of the objective F and the corresponding 2x1 gradient G.
The first I get by doing
F=norm((1/2)*(sum(y0_all)).^2)
which gives the scalar value of F. However, the correspondikng 2x1 gradient G is not returned. What is the right command? Do anyone have a link to a MATLAB page that shows the relevant command?
Thanks

 採用された回答

Voss
Voss 2024 年 4 月 4 日
編集済み: Voss 2024 年 4 月 4 日

1 投票

% take a look at what enzymeGeneral.m is
type enzymeGeneral.m
function [result,grad_all] = enzymeGeneral(k) N = 4; % number of experiments result = zeros(2*N,1); % pre-allocate result arrays grad_all = zeros(2*N,2); dt=0.01; T=1; %k=[5;1]; y0_all = [2 1 1 4; 1 1 0 1]; y1_all = [1.0 0.5 0.3 2.4; 2.1 1.6 0.9 2.7]; for ii = 1:N y0 = y0_all(:,ii); y1 = y1_all(:,ii); [SP,grad]=enzyme(y0,k,dt,T); result((ii-1)*2+[1 2]) = y1-SP; grad_all((ii-1)*2+[1 2],:) = -grad; end F = (1/2)*(sum(y0_all)).^2 G = (sum(y0_all)).*(y1_all).*(y0_all)
If you want enzymeGeneral to return the F and G it calculates, change its first line to:
function [F,G] = enzymeGeneral(k)
But then, result and grad_all are calculated for no reason and the input k is unused, so I'm not sure if that's what you want.
Anyway, here's the relevant documentation:

6 件のコメント

Sergio
Sergio 2024 年 4 月 4 日
編集済み: Sergio 2024 年 4 月 4 日
F and G are only the names of the vector scalar product and 2x1 gradient and k is generated in test-modified. So this can be ignored.
Voss
Voss 2024 年 4 月 4 日
編集済み: Voss 2024 年 4 月 4 日
What outputs should enzymeGeneral have?
If the only outputs are F and G, then you only need y0_all and y1_all; there is no need to calculate result and grad_all because they are not used in the calculation of F and G. If you are not calculating result and grad_all, then you don't need k (or anything else besides y0_all and y1_all).
Sergio
Sergio 2024 年 4 月 4 日
編集済み: Sergio 2024 年 4 月 4 日
@Voss . That is correct. I just had those other inputs since I used the same structure from before.
The problem is that the sum/gradient command I use in :
G = sum((y1_all).*gradient((y1_all)))
and it should have refelcted this:
where r_i(k) is y1_all
which is supposed to give a 2x1 matrix, gives a 1 x 5 vector.
Voss
Voss 2024 年 4 月 4 日
G = sum(y1_all.*gradient(y1_all), 2)
% ^ sum over the 2nd dimension
Sergio
Sergio 2024 年 4 月 4 日
Ah, thanks that solved the problem!
Voss
Voss 2024 年 4 月 4 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

製品

リリース

R2023b

質問済み:

2024 年 4 月 4 日

コメント済み:

2024 年 4 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by