MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Finding where curves are approximately equal to zero

Asked by pxg882 on 8 Nov 2012

Hi all,

I need to find where a collection of curves are all approximately equal to zero (given some tolerance level) and was wondering if anyone could help me?

I'm running the following script:

dF2=0.001;
dG2=0.001;
initF2=0.522;
initG2=-0.603;
K=zeros(2);
etaspan=[0 20];
H=[1;1];
options=odeset('AbsTol',1e-7,'RelTol',1e-4);

while max(abs(H))>1e-8

    [eta,X]=ode45(@nN,etaspan,[0;initF2+dF2;1;initG2;0],options);
n=size(eta,1);
X2=[X(n,1);X(n,3)];
[eta,X]=ode45(@nN,etaspan,[0;initF2;1;initG2+dG2;0],options);
n=size(eta,1);
X3=[X(n,1);X(n,3)];
[eta,X]=ode45(@nN,etaspan,[0;initF2;1;initG2;0],options);
n=size(eta,1);
X1=[X(n,1);X(n,3)];
    K(1,1)=(X2(1)-X1(1))/dF2;
K(2,1)=(X2(2)-X1(2))/dF2;
K(1,2)=(X3(1)-X1(1))/dG2;
K(2,2)=(X3(1)-X1(2))/dG2; 
    H=K\-X1;
initF2=initF2+H(1);
initG2=initG2+H(2);
end

figure;
hold all;
plot(eta,X(:,1));
plot(eta,X(:,3));
plot(eta,(-1)*X(:,5));
plot(eta,X(:,2));
plot(eta,X(:,4));
hold off;
xlabel('\eta')
hleg = legend('F','G','-H','F\prime','G\prime','Location','SouthEast');

disp('Value of F''(0)')
a = X(1,2);
disp(a)

disp('Value of G''(0)')
b = X(1,4);
disp(b)

disp('Value of H(20)')
c = X(end,5);
disp(c)


Calling the function:

function Y=nN(x,X)

n=1.3;

dF1deta=X(2);

dF2deta=n^(-1)*((X(2)^(2)+X(4)^(2))^((n-1)/2))^(-1)*((X(1)^(2)-X(3)^(2)+(X(5)+((1-n)/(n+1))*X(1)*x)*X(2))*(1+(n-1)*(X(2)^(2)+X(4)^2)^(-1)*X(4)^(2))-(n-1)*X(2)*X(4)*(X(2)^(2)+X(4)^(2))^(-1)*(2*X(1)*X(3)+(X(5)+((1-n)/(n+1))*X(1)*x)*X(4)));

dG1deta=X(4);

dG2deta=n^(-1)*((X(2)^(2)+X(4)^(2))^((n-1)/2))^(-1)*((2*X(1)*X(3)+(X(5)+((1-n)/(n+1))*X(1)*x)*X(4))*(1+(n-1)*(X(2)^(2)+X(4)^2)^(-1)*X(2)^(2))-(n-1)*X(2)*X(4)*(X(2)^(2)+X(4)^(2))^(-1)*(X(1)^(2)-X(3)^(2)+(X(5)+((1-n)/(n+1))*X(1)*x)*X(2)));

dH1deta=-2*X(1)-(1-n)/(n+1)*x*X(2);

Y = [dF1deta; dF2deta; dG1deta; dG2deta; dH1deta];


From the plot that is produced I would like to find where the curves for F, F', G and G' are approximately equal to zero given a tolerance of 1e-6. Simply from looking at the plot I can see that this is around eta~5.7 but I would like to be able to print out this value of eta.

Is this possible?

Any help would be greatly appreciated.

1 Answer

Answer by Pedro Villena on 9 Nov 2012
Accepted answer

Add these lines at the end of your script code:

tol = 1e-6; %tolerance
idx = find((abs(X(:,1))<tol) & (abs(X(:,2))<tol) &...
(abs(X(:,3))<tol) & (abs(X(:,4))<tol),1);
hold on,
plot(eta(idx),0,'ko');
title(sprintf('\\eta_0 = %.3f',eta(idx)))
hold off,


Contact us