Undefined function or variable 'filename'.
이전 댓글 표시
Hi,
I've written some functions to numerically integrate the equations of motion of a double pendulum. It is a first order differential equation (like ydot = f(t,y)). I am writing my own numerical integration (as part of an assignment). Now here's the thing:
The matrices used to solve for ydot (the derivative of the state) are created with the symbolic toolbox. Because I do not want to use 'subs' each time before I solve, because it is very slow, I have used 'diary' to create an m-file frmatrix.m that simply states: fr = [blabla]; So when I run it, with the variables in my workspace, it simply creates the matrix fr. I do this for an Mr and an fr because I want to calculate Mr\fr. The lines are below eachother but I get an error for fr: Undefined function or variable 'frmatrix'. It has done exactly the same for 'Mrmatrix' which doesn't fail. I have checked and all.. the name is correct and frmatrix.m exists. Now here's the thing: if I restart matlab, it works fine. If i put a keyboard in the loop so I have to press f5 on every iteration, it works. But it will work only once after restarting matlab. if i run it again, i get the error again. even if i delete frmatrix.m
I really have no clue why this is happening so I hope anyone can help me out?
Kind regards, Iris
ps. below is a minimum working example, I hope it is clear. It consists of 2 separate m-files.
function tmt(par)
syms m I l g
syms p1 p2 p1d p2d
%%%%%%%Symbolic Mr and fr are created here, I left it out for simplicity %%%%%%%
% variables m,I,l and g are given a value
m = par.m;
I = par.I;
l = par.l;
g = par.g;
% The symbols of m, I, l and g in Mr and fr are subsituted with the values
Mr = subs(Mr);
fr = subs(fr);
% the files Mrmatrix.m and frmatrix.m are created.
if exist('Mrmatrix.m', 'file')
! del Mrmatrix.m
end
diary Mrmatrix.m
disp('Mr = ['), disp(Mr), disp('];');
diary off
if exist('frmatrix.m', 'file')
! del frmatrix.m
end
diary frmatrix.m
disp('fr = ['), disp(fr), disp('];');
diary off
function statederivative = eom(t,state,par)
p1 = state(1);
p2 = state(2);
p1d = state(3);
p2d = state(4);
Mrmatrix;
frmatrix; % HERE IT FAILS!!!
qdd = double(Mr\fr);
statederivative = [state(3); state(4); qdd];
댓글 수: 2
Star Strider
2014년 4월 21일
Is there some reason you must use the Symbolic Math Toolbox here rather than using ode45?
Iris
2014년 6월 26일
채택된 답변
추가 답변 (1개)
Walter Roberson
2014년 4월 21일
You need to
rehash
if you create a .m on the fly and want to execute it.
댓글 수: 2
Walter Roberson
2014년 4월 22일
Faster than a general rehash would be to
clear frmatrix
Iris
2014년 6월 26일
카테고리
도움말 센터 및 File Exchange에서 Special Values에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!