Autogenerating fuctions from syms-Expression
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Suppose I have a symbolic expression in Matlab that contains a vector, for example:
syms v1;
syms v2;
vector(1) = sin(v1)+cos(v2);
vector(2) = 1-sin(v1);
Is there a easy way to automatically make a function out of only the variable "vector" that looks something like this?
function vector = myFunction(v1,v2)
vector(1) = sin(v1)+cos(v2);
vector(2) = 1-sin(v1);
end
Of course, it's pointless in this example. I only have a relatively large matrix with long entries instead of the vector.
It would save me a lot of typing.
Antworten (1)
Chidvi Modala
am 16 Jul. 2019
I understood that you wanted to create a function which can automatically create equations and store them in a variable ‘vector’.
The below code will help you out
function vector=myFunc
vector{1}=@(v1,v2)(sin(v1)+cos(v2));
vector{2}=@(v1)(1-sin(v1));
end
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!