I run a matlab code. I get the plots but do not get the variables in workspace. Somebody please help me to fix this.

1 次查看(过去 30 天)
I am running the following matlab code.
function whisker_curvature
clc
clear all
%global t x y
x=[1 1 1 1 1 1 1];
% options=optimset('Display''iter');
x1=fsolve(@solver,x);
end
function F=solver(x)
%options = odeset('RelTol',1e-8,'AbsTol',[1e-5, 1e-5, 1e-5, 1e-5, 1e-5, 1e-5]);
[t,y] = ode45(@equation,[0, 1], [0 0 0 x(1) x(2) x(3) 0]);%,options); % theta-zero_0 is also OK/
s = length(t);
F = [y(s,1)-14.96, y(s,2)-4.48, y(s,3)- 30];%, u(s,4), u(s,5), u(s,6)]; %at theta(1)=11, we have intersting behavior.
figure(1)
plot(t,y(:,1),'--ro',t,y(:,2),'--bo',t,y(:,3), '--g',t,y(:,4),t,y(:,5), '--b',t,y(:,6), 'go',t,y(:,7), '--r') % ,t,y(:,2),'--bo',t,y(:,3),t,y(:,4),t,y(:,5),t,y(:,6), 'go'
% plot(t,y(:,1),'--ro')
%plot(t,y(:,2),'--bo')
xlabel('sigma')
ylabel('unknowns')
%grid on
end
function dy = equation(t,y) % t is my \sigma and unknowns (x,y,\theta,F,\theta_{obj},s_{obj},x_u)=(y(1),y(2),y(3),y(4),y(5),y(6),y(7))
L = 20;
dy = zeros(7,1);
dy(1)=y(6)*cos(y(3));
dy(2)=y(6)*sin(y(3));
dy(3)=2*0.02/(1+(2*0.02*y(7))^2)^(3/2)+(y(6)*y(4))/3*(pi/4*((L-y(6)*t)/L)^4)*((15.13-0.25*sin(y(5))-y(1))*cos(y(5))+(4.29+0.25*cos(y(5))-y(2))*sin(y(5)));
dy(4)=0;
dy(5)=0;
dy(6)=0;
dy(7)=y(6)*1/(sqrt(1+(2*0.02*y(7))^2));
end
I get the plots but do not get the variables value in the workspace window.
I suspect this happens due to the following error message received:
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead. > In fsolve (line 298) In whisker_curvature (line 7)
Solver stopped prematurely.
fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 1400 (the default value).
Someone please help me to fix this problem.
  5 个评论
Jan
Jan 2017-10-26
I've edited the question and applied the formatting now.
The "clear all" on top of a function is completely meaningless. It deteles all variables of the current workspace, but on top of a function the (local!) workspace is empty at all. In addition clear all removes all functions from the memory, and reloading them from the disk is a waste of time. Simply omit the darn clearing. It does not help.

请先登录,再进行评论。

回答(2 个)

Niklas Nylén
Niklas Nylén 2017-10-26
Variables created in a function do not get assigned to the base workspace. To debug the variable values you should add a debug stop in your function. When the debugger is stopped the function you will have access to the function's local variables.

Jan
Jan 2017-10-26
The warning tells you, that the default "Trust-region-dogleg" cannot be applied to your problem. You can use optimset to enable a suiting method, e.g. the chosen "Levenberg-Marquardt".
It sounds strange, that fsolve stops with an error, but you still get plots. The message tells you, that the maximum number of function evaluations is exceeded. Either it is a bug in the formula, or the problem does not converge, or you have to increase the number of accepted function evaluations.
  5 个评论
Jan
Jan 2017-10-27
Then let me encourage you to read the documentation of your Matlab versions. In the current version (and the online documentation) the command is called "optimoptions", but in your version it might be "optimset".

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by