Strange behaviour while running the script

4 次查看(过去 30 天)
Hello.
I'm using MATLAB R2017a. I literally have no idea why the MATLAB works like this but I didn't seem to face it ever before.
I created a script file and creat some functions INSIDE this .m file. One of the functions solves parabolic equation. When I first compile the script it gives an answer like "root(..., .., 1); root(..., .., 2); root(..., .., 3)"; So, I mean, there're three roots. Then I don't change anything and do literally nothing but compile it for the second time. And at the second time it give ONE root instead of THREE even though as I said I had changed nothing! If I put "clear all" into the command line and then run the script, it again gives three roots. But if I launch it afterwards I get ONE root again. Why?
On this screenshot the result after the first compiling. The arrow show there are three roots.
repeat I'm chaning nothing before this moment!
function draft14
sys = @(x,y,z,c,mu1,s1,r2,b,s2) [c*y - mu1*x + x*z + s1; ...
r2*y*(1 - b*y) - x*y; ...
z - x*y + s2];
c = 0.2;
mu1 = 0.5;
s1 = 0.1;
r2 = 0.9;
b = 0.5;
s2 = 0;
par = [c mu1 s1 r2 b s2];
f = @(t,x) fsys(x(1),x(2),x(3),par(1),par(2),par(3),par(4),par(5),par(6));
tspan = 0:0.01:2;
y0 = [0.1 0.9 0.1];
[~,Y] = ode45(f,tspan,y0);
plot3(Y(:,1),Y(:,2),Y(:,3)); hold on;
plot3(y0(1),y0(2),y0(3), 'o','LineWidth',3);
xlabel('X'); ylabel('Y'); zlabel('Z');
figure;
plot(Y(:,1),Y(:,2));
xlabel('X'); ylabel('Y');
cubeq(par);
eqpoints(par);
conds(par);
end
function cubeq(par) %Решает численно кубическое уравнение
c = par(1);
mu1 = par(2);
s1 = par(3);
r2 = par(4);
b = par(5);
s2 = par(6);
syms x
f = x^3 - r2*x^2 + (c + mu1*r2*b)*x - r2*(b*s1 + c);
s = solve(f,x);
disp('Решение кубического уравнения:');
disp(s);
end
function eqpoints(par) %Считает положения равновесия
c = par(1);
mu1 = par(2);
s1 = par(3);
r2 = par(4);
b = par(5);
s2 = par(6);
fprintf('c = %g, mu1 = %g, s1 = %g, r2 = %g, b = %g, s2 = %g \n',...
par(1),par(2),par(3),par(4),par(5),par(6));
syms x y z positive
F = [c*y - mu1*x + x*z + s1; r2*y*(1 - b*y) - x*y; z - x*y + s2];
eq = solve(F, [x y z]);
disp(eq.x);
eq = double([eq.x eq.y eq.z]');
disp('Положения равновесия:');
disp(eq);
end
function conds(par) %Проверяет условия на правое значение параболы и число корней
c = par(1);
mu1 = par(2);
s1 = par(3);
r2 = par(4);
b = par(5);
s2 = par(6);
v = s1 - mu1*r2;
fprintf('v_cubicpar = %g \n', v);
v = (c + b*mu1*r2)^2*((4/27)*(c+b*mu1*r2)-(3/81)*r2^2)...
- r2^2*(b*s1 + c)*((2/3)*(c + b*mu1*r2) + (4/27)*r2^2 + 1);
fprintf('v_roots = %g \n', v);
end
P.S. Sorry for the cyrillic text on the comments and the screens.
  1 个评论
Jan
Jan 2017-5-24
As far as I can see, you do not post the code of the script: You say, that you have a script, with functions in it, but all I see are functions. What exactly does "compile" mean?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-5-24
In the first function you have
syms x
In the second function you have
syms x y z positive
This creates assumptions that are not removed when the function exits.
The Symbolic Engine exists as a separate entity that can be talked to from any function. Any change you make to the state of the Symbolic Engine is kept until you clear or reset the Symbolic Engine. So, for example, if you were to tell the symbolic engine to define a function in one routine, then it would still know the function and be able to use it in another routine. When you set an assumption in one routine, it remembers the assumption...
  1 个评论
MGMKLML
MGMKLML 2017-5-24
编辑:MGMKLML 2017-5-24
Thank you, I got it. I cleared all the assumptions I had and cleared the variables as well. Then typed "clear all" to delete all the memory about the last compiling and then ran it 3-4 times in a row. Now it works properly. Thank you!

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by