multiple function in one .m file
Ältere Kommentare anzeigen
Hello, I am trying to input multiple function in my assignment using integral with the trapz command. This is one of my functions:
EDITOR:
function z= myfun2(t)
z(1) = 10*t;
end
COMMAND WINDOW:
t = linspace(0,1,400);
z = myfun(t)
area = trapz(t,z)
Now, I have multiple functions to input with different values of t. how do I put them all in one .m file on the EDITOR for expample:
function z= myfun2(t)
z(1) = 10*t; the value of to here will be t= linspace (0,1,400);
z(2) = 10*t.^2; the value of to here will be t= linspace (-1,2,400);
z(3) = 5*exp.^(-2*t); the value of to here will be t= linspace (-1,1,400);
end
How can I set this in one .m file with different values of t and obtain the results in my COMMAND WINDOW for all of them?
Akzeptierte Antwort
Weitere Antworten (3)
The first function shows a basic misunderstanding of MATLAB's indexing and arrays, which has nothing to do with functions per se. Lets have a look:
function z= myfun(t)
z(1) = 10*t;
end
is called from the command window with this (after fixing the incorrect function name):
>> t = linspace(0,1,400);
>> z = myfun(t)
However this immediately produces an error, proving that you did not even try the code that you posted:
In an assignment A(I) = B, the number of elements in B and I must be the same.
What does this mean? This is because t is a vector of values (it has 400 elements!), whereas inside myyfun you are string to put 400 elements in one element z(1) with this allocation:
z(1) = ... 400 elements
This will always be an error in MATLAB: every element must have its own position. One solution is to use some indexing , but the best options is to simply ignore the indexing altogether:
z = ... 400 elements
This will then work without error.
But how can we fix the second function? In this case you actually want to use different t values on each function. There are many solutions to this, here are two that you could try:
1) an easy but not very good way would be to return one numeric matrix for each call of the function, with all values inside:
function z= myfun2(t)
z(1,:) = 10*t;
z(2,:) = 10*t.^2;
z(3,:) = 5*exp(-2*t); % fixed syntax mistake with exp.
end
2) a much better solution is to return function handles for each function, and evaluate these in the command window:
function fun = myfun2
fun{1} = @(t) 10*t;
fun{2} = @(t) 10*t.^2;
fun{3} = @(t) 5*exp(-2*t); fixed syntax mistake with exp
end
calling this function will return a cell array of functions handles : these can then be evaluated with whatever t values you want:
>> fun = myfun2();
>> y = fun{2}(t) % evaluates the second function
3 Kommentare
ddd ppp
am 13 Apr. 2018
awesome answer
Venky Suriyanarayanan
am 31 Jul. 2018
Fantastic answer..!! Both the methods work like a charm though I eventually ended up using method (1) because it suited my project requirements better. Thanks a lot for taking out time and explaining in such great details.
P Lepage
am 2 Nov. 2020
we the people demand that this answer is accepted
Steven Armour
am 13 Okt. 2016
9 Stimmen
You can just switch to python or any of the other C-based languages; where this is not a problem
2 Kommentare
Walter Roberson
am 13 Okt. 2016
Your remark is not correct. The external visibility of multiple functions in a single source code in C is controlled by the linker, the workings of which is outside the C standard. Linkers can have complicated rules about visibility, often requiring that control files be built to describe the visibility; the same control file might or might not also be used to control address layouts or absolute addresses that items need to be linked at. It is a problem.
Moshe Flam
am 20 Nov. 2017
Bearbeitet: Moshe Flam
am 20 Nov. 2017
Maybe. But none of the millions of c programmers ever encountered that setting, let alone know about it. So the snide remark holds water.
Cezar-Grigore Dihel
am 25 Feb. 2022
6 Stimmen
idk bro sorry
Kategorien
Mehr zu Call Python from MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!