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

조회 수: 3 (최근 30일)
Meenakshi Tripathi
Meenakshi Tripathi 2021년 4월 5일
댓글: Rik 2021년 4월 5일
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
Rik
Rik 2021년 4월 5일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2021년 4월 5일
편집: Matt J 2021년 4월 5일
lmimeena must be told what to return in the declaration line,
function L=lmimeena(A,C)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by