SUbstitions

Hello,
I have a vector of 15 elemetnts which are functions of 4 elements, here below represented:
h(i)=[1, csi_1, csi_2, csi_3, csi_4, csi_1^2 - 1, csi_1*csi_2, csi_1*csi_3, csi_1*csi_4, csi_2^2 - 1, csi_2*csi_3, csi_2*csi_4, csi_3^2 - 1, csi_3*csi_4, csi_4^2 - 1]
I have 15 different vectors with the valus which those variables should have. E.g. s=[1 2 3 4] gives:
[1, 1, 2, 3, 4, 0, 2, 3, 4, 3, 6, 8, 8, 12, 15]
DO you have any suggestion how to impose for the 15 different cases the different values of csi_i in order to obtain a matrix of only coefficients.
At the moment the expression in csi_i are in the form h(1)=1, h(2)=csi_1, h(3)=csi_2 etc...
I tried with subs but I did not succeed.
THank you very much for your help and suggestions! ANtonio

 Risposta accettata

Matt Fig
Matt Fig il 25 Mag 2011

0 voti

M = @(csi) [1, csi(1), csi(2), csi(3), csi(4), csi(1)^2 - 1,...
csi(1)*csi(2), csi(1)*csi(3), csi(1)*csi(4),...
csi(2)^2 - 1, csi(2)*csi(3), csi(2)*csi(4),...
csi(3)^2 - 1, csi(3)*csi(4), csi(4)^2 - 1];
s=[1 2 3 4];
M(s) % You can use this directly or assign the results to an array.
%
%
%
% EDIT
If you want to be able to do it all at once for n-by-4, then:
M = @(csi) [ones(size(csi,1),1), csi(:,1), csi(:,2), csi(:,3), csi(:,4),...
csi(:,1).^2 - 1,csi(:,1).*csi(:,2), csi(:,1).*csi(:,3),...
csi(:,1).*csi(:,4), csi(:,2).^2 - 1, csi(:,2).*csi(:,3),...
csi(:,2).*csi(:,4), csi(:,3).^2 - 1, csi(:,3).*csi(:,4),...
csi(:,4).^2 - 1];
s = rand(15,4);
M(s)

8 Commenti

mortain Antonio
mortain Antonio il 25 Mag 2011
Dear Matt, I need to make 15 vectors of M, since I have 15 different vectors of M. With MySetperms I get arrays of 4 columns and then I use each array in M. I wrote:
for i=1:15
M(i) = @(csi_v) [1, csi_v(1), csi_v(2), csi_v(3), csi_v(4), csi_v(1)^2 - 1,...
csi_v(1)*csi_v(2), csi_v(1)*csi_v(3), csi_v(1)*csi_v(4),...
csi_v(2)^2 - 1, csi_v(2)*csi_v(3), csi_v(2)*csi_v(4),...
csi_v(3)^2 - 1, csi_v(3)*csi_v(4), csi_v(4)^2 - 1];
s=MySetperms(i,:);
end
but gives me: ??? Nonscalar arrays of function handles are not allowed; use cell arrays instead.
Error in ==> ProvaDiff at 119
M(i) = @(csi_v) [1, csi_v(1), csi_v(2), csi_v(3), csi_v(4), csi_v(1)^2 - 1,...
In addition, is there a manner to avoid writing [1, csi_v(1) etc...and directly write [H] which contains all the arrays?
Thanks a lot!
Matt Fig
Matt Fig il 25 Mag 2011
This M is not what I gave you! Use M instead of M(i). M is a function handle, to be used as I showed above. Look over the examples I gave you. M returns a vector, which you can store in a cell array or as a row in a matrix.
Matt Fig
Matt Fig il 25 Mag 2011
Define M outside the loop as I showed you above, then:
for ii = 1:15
s=MySetperms(ii,:);
R(ii,:) = M(s); % We actually don't need s here, but it's o.k.
end
Matt Fig
Matt Fig il 25 Mag 2011
I assumed above that MySetperms is 15-by-4...
mortain Antonio
mortain Antonio il 25 Mag 2011
Got it! It has a different sintax, what's the name of this syntax in the documentation?
What about trying to avoid to write all the coefficients
[1, csi_v(1), csi_v(2), csi_v(3), csi_v(4), csi_v(1)^2 - 1,...
csi_v(1)*csi_v(2), csi_v(1)*csi_v(3), csi_v(1)*csi_v(4),...
csi_v(2)^2 - 1, csi_v(2)*csi_v(3), csi_v(2)*csi_v(4),...
csi_v(3)^2 - 1, csi_v(3)*csi_v(4), csi_v(4)^2 - 1] since they are actually 35 (I wrote a simpler version of the system)
I have in H written all of them in the way I wrote them in the first post..let me have some suggestions. I did not get why you had to change the writings from
[1, csi_1, csi_2, csi_3, csi_4, csi_1^2 - ...
to
1, csi_v(1), csi_v(2), csi_v(3), csi_v(4), csi_v(1)^2 -
(My vector is called csi_v yours csi, I changed your name but doesn't change the shape, just the name)
Again thanks for help!
The rough version is working. I need just to fix this thing I just asked you and another one which requires a bit more time...
THanks again!
Matt Fig
Matt Fig il 25 Mag 2011
I changed it from csi_1 to csi(1) etc. because M is a function handle that takes a n-by-4 vector as an argument. Thus inside the function, it takes the first value in the vector where you see csi(1), the second value in the vector where you see csi(2), etc.
To learn more about anonymous functions, type this:
docsearch('anonymous functions')
Then hit return.
mortain Antonio
mortain Antonio il 25 Mag 2011
hit return it's the most important thing :-)
SOrry for being so annoying.....I know I should handle such stupid things on my own!
Shall I send to you the whole program, to see if it's possible to use H instead of hard coding the csi(1), csi(2) etc in M ?
Again, thanks for patience!
Matt Fig
Matt Fig il 26 Mag 2011
I'll take a look at it.

Accedi per commentare.

Più risposte (2)

mortain Antonio
mortain Antonio il 25 Mag 2011

0 voti

I tried to use your answer but my program does not allow me anymore to do the derivation (here reported one) can you help with that, please?
csi_1=sym('csi_1');
csi_2=sym('csi_2');
csi_3=sym('csi_3');
csi_4=sym('csi_4');
csi_v(1)=csi_1;
csi_v(2)=csi_2;
csi_v(3)=csi_3;
csi_v(4)=csi_4;
b=sum(diag((csi_v).'*csi_v));
H(1)=1;
i=1;
H(2)=(-1)^(i)*exp(0.5*b)*diff(exp(-0.5* b),csi_1);
H(3)=(-1)^(i)*exp(0.5*b)*diff(exp(-0.5* b),csi_2);
H(4)=(-1)^(i)*exp(0.5*b)*diff(exp(-0.5* b),csi_3);
and so on with other derivations to get the coefficients up.
Thank you very much. If it is off topic I'll open another question.
That is the message:
??? The following error occurred converting from sym to double:
Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double
array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in ==> ProvaDiff at 76
H(2)=(-1)^(i)*exp(0.5*b)*diff(exp(-0.5* b),csi_1);

2 Commenti

Matt Fig
Matt Fig il 25 Mag 2011
You didn't mention that you wanted to use this symbolically in your original post. This makes a world of difference! I don't have the symbolic toolbox, but with any luck Walter, someone else who has the symbolic toolbox will be along to help with this new issue.
mortain Antonio
mortain Antonio il 25 Mag 2011
Sorry I haven't mentioned. I was used to Maple or Fortran where there is no difference

Accedi per commentare.

Walter Roberson
Walter Roberson il 25 Mag 2011

0 voti

H(1) = 1;
creates H as an array of double. Two lines later you try to assign a symbolic result in to H(2) so the program tries to convert that symbolic result in to a double.
I suggest that above the H(1) assignment, you have
H = repmat(sym(0),1,4);

1 Commento

mortain Antonio
mortain Antonio il 25 Mag 2011
Your suggestion was really helpful since I've understood where was the problem. I solved it by putting H(1)=1 at the end!
Thanks again

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by