calling functions in other scripts
2 views (last 30 days)
Show older comments
Hi, I want call this function :
classdef functioncontainer
methods
function res = func(t,n)
%Conservation de la masse:
%C:
masse_C(:)= n(:,1) + 2*n(:,9)+2*n(:,10)+3*n(:,13)+...
n(:,22)+n(:,26)+n(:,34)+2*n(:,35)+n(:,36)...
+n(:,37)+2*n(:,39)+4*n(:,40)+5*n(:,41)
%O:
masse_O(:)= n(:,4)+2*n(:,15)+2*n(:,16)+2*n(:,17)+n(:,22)...
+n(:,24)+ 3*n(:,29)+n(:,30)+2*n(:,32)+2*n(:,34)+...
n(:,36)+n(:,37)+3*n(:,38)+ n(:,39);
%N
masse_N(:)= n(:,7)+2*n(:,20)+n(:,24)+n(:;26)+2*n(:,30)+n(:,32)...
+n(:,35)+n(:,36)+n(:,37)+n(:,38)
in this script
...............
tic,[t,n] = ode45(@(t,n)myequations(t,n,k1,k26,k57,k58,k59...),[0 10e-3],IC)
toc
plot(t,n)
myobj= functioncontainer
res1=myobj.func(t,n)
I don't know what is missing .
Elapsed time is 1.103608 seconds.
Error using functioncontainer
Error: File: functioncontainer.m Line: 15 Column: 44
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Error in finalODE (line 204)
myobj= functioncontainer
7 Comments
Torsten
on 30 May 2021
Just a sidenote: I wonder why you want to use object-oriented programming when you just intend to check some mass balances. Don't you think it's kind of overkill ?
Answers (1)
Steven Lord
on 30 May 2021
This is not just a function. It is a class method.
classdef functioncontainer
methods
function res = func(t,n)
As written, either t or n must be a functioncontainer object. It's not really clear to me how you're trying to use this (your code excerpt refers to "myequations" which is not defined in that excerpt) as your ODE function.
But I concur with Torsten. In this case using a class seems unnecessary. Instead I would simply write a function like the two examples on this documentation page to evaluate the right hand side of your system of ODEs and call it in a similar manner as those examples do. I would also advise you to extract some or all of the components of the second input to variables with descriptive names at the start of your file as that may make your code look more like the equations in the textbook or paper that you're trying to implement.
function dydt = mysystem(t, y)
% Concentrations
O2conc = y(1);
CO2conc = y(2);
% etc.
% now work with O2conc, CO2conc, etc.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!