Is it possible to creare a matrix of functions?
136 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello, i am working on a code for my university. Is it possible to create a vector of 229 functions in a way that if i select an element, for exemple tc(3), as in the image, i can work on the 3st function of my vector. Thank you very much!
10 Kommentare
Guillaume
am 2 Mai 2019
Bearbeitet: Guillaume
am 2 Mai 2019
Note that there's never any point(*) to write numbers with that many digits:
t=0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333:0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333:76.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333;
A number of class double can only store about 16 different digits and matlab will ignore the extra digits.
t = 0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333
%and
t = 0.3333333333333333
%and the simpler
t = 1/3
will all result in the exact same value stored in memory (which if decoded in decimal by James Tursa's excellent num2strexact is 0.333333333333333314829616256247390992939472198486328125, the best approximation to 1/3 you can get with double)
The best way to write your expression is thus:
t = 1/3 : 1/3 : 76+1/3;
------
* others than for messing up the formatting of activity feed on this forum, it turns out!
Antworten (1)
John D'Errico
am 1 Mai 2019
Bearbeitet: John D'Errico
am 1 Mai 2019
Do you want to create function handles? For example,
fmat = {@(x) sin(x), @(x) cos(x), @(x) exp(x), @(x) x^2 + 1};
fmat{1}(2)
ans =
0.9093
fi = fmat{2}
fi =
function_handle with value:
@(x)cos(x)
Or you could create an array of symbolic objects.
syms x
fmat = [x, x^2+2, (x+1) -2*x];
then you could use tham as functions too.
matlabFunction(fmat(1))
ans =
function_handle with value:
@(x)x
A virtue of an array of symbolic objects is you can do many things with them all.
int(fmat,x)
ans =
[ x^2/2, (x*(x^2 + 6))/3, (x*(x + 2))/2, -x^2]
And you can even use them all at once, in one call.
fall = matlabFunction(fmat)
fall =
function_handle with value:
@(x)[x,x.^2+2.0,x+1.0,x.*-2.0]
>> fall(3)
ans =
3 11 4 -6
4 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!