how to call function from one m file to another m file?

3 views (last 30 days)
function homework
clc
clear all
A = [0 1;0 0];
B = [0;1];
C = [1 0];
L = lmimeena(A,C);
tspan = [0:.1:10];
x0 = [1 0];
[~,x] = ode45(@basic,tspan,x0)
y = x*C'
function dxdt = basic(t,x)
A = [0 1;0 0];
B = [0;1];
dxdt = A*x+B*[t^2]
end
x_cap(1,:) = [10 11];
x_cap1 = x_cap(1,:);
x_cap2 = x_cap1
for i = 1:length(y)-1;
t_temp = ((i-1)/10):.05:(i/10);
[~,x_cap] = ode45(@observer,t_temp,x_cap(end,:))
x_cap1 = [x_cap1;x_cap(end,:)]
x_cap2=[x_cap2;x_cap(2,:);x_cap(end,:)]
end
function dx_capdt = observer(t,x_cap);
dx_capdt=(A-L*C)*x_cap+L*[(y(i,:)+y(i+1,:))/2]'+B*[t^2];
end
e=x-x_cap1;
%%%%%%%%%%%%%%%%%%%%%%%%%%
function lmimeena(A,C)
clear all
clc
A = [0 1;0 0];
C = [1 0];
setlmis([])
P = lmivar(1,[size(A,1) 1]);
K = lmivar(2,[2 1]);
lmiterm([1 1 1 P],1,A,'s');
lmiterm([1 1 1 K],-1,C);
lmiterm([1 1 2 0],1);
lmiterm([1 2 2 P],-1,1);
LMISYS = getlmis;
[tmin,Psol]=feasp(LMISYS);
[tmin,Ksol]=feasp(LMISYS);
P = dec2mat(LMISYS,Psol,P)
K = dec2mat(LMISYS,Ksol,K)
L = inv(P)*K
end
I am unable to call function lmimeena to function homework.
the error i am getting is
Error using lmimeena
Too many output arguments.
Error in homework (line 7)
L = lmimeena(A,C)
  1 Comment
Rik
Rik on 5 Apr 2021
You should avoid clear in any function, and clear all should exist in only 1 function in your entire code base as part as a script that essentially restarts Matlab.
You should also use ; to supres outputs and use functions like disp and fprintf to show variable content. Code that doesn't print anything to the command window should not contain clc.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 5 Apr 2021
Edited: Matt J on 5 Apr 2021
lmimeena must be told what to return in the declaration line,
function L=lmimeena(A,C)

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!