Functions close to zero in MATLAB

In summary, the conversation discusses finding where a collection of curves are all approximately equal to zero, given a tolerance level of 1e-6. The code being used is solving a system of differential equations using the ode45 function in MATLAB. To find the values of eta where the curves are approximately equal to zero, the find function can be used to find the indices of the elements that meet the condition. These indices can then be used to find the corresponding values of eta and print them out.
  • #1
Juggler123
83
0
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 in MATLAB:

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.
 
Physics news on Phys.org
  • #2



Hi there,

It looks like your code is solving a system of differential equations using the ode45 function in MATLAB. In order to find where the curves are approximately equal to zero, you can use the find function in MATLAB. This function will return the indices of the elements in a vector that meet a certain condition. In your case, you can use it to find the indices where the values of F, F', G, and G' are less than the tolerance level of 1e-6. Then, you can use these indices to find the corresponding values of eta using the eta vector from your ode45 function. Here is an example of how you could do this:

% Find the indices where the values of F, F', G, and G' are less than the tolerance level
indF = find(abs(X(:,1)) < 1e-6);
indFprime = find(abs(X(:,2)) < 1e-6);
indG = find(abs(X(:,3)) < 1e-6);
indGprime = find(abs(X(:,4)) < 1e-6);

% Find the corresponding values of eta
etaF = eta(indF);
etaFprime = eta(indFprime);
etaG = eta(indG);
etaGprime = eta(indGprime);

% Print out the values of eta where the curves are approximately equal to zero
disp('Value of eta where F is approximately equal to zero:')
disp(etaF)
disp('Value of eta where F'' is approximately equal to zero:')
disp(etaFprime)
disp('Value of eta where G is approximately equal to zero:')
disp(etaG)
disp('Value of eta where G'' is approximately equal to zero:')
disp(etaGprime)

I hope this helps! Let me know if you have any further questions.
 

FAQ: Functions close to zero in MATLAB

What are functions close to zero in MATLAB?

Functions close to zero in MATLAB refer to mathematical functions that have values very close to zero. These functions are often used to solve optimization problems or to find the roots of equations. In MATLAB, functions close to zero are typically represented as small decimal values, such as 1e-6 or 1e-9.

How can I find functions close to zero in MATLAB?

To find functions close to zero in MATLAB, you can use the built-in function fzero. This function takes in an initial guess for the root of the function and returns the value of the root. You can also use the roots function to find the roots of polynomial functions, which may include functions close to zero.

What is the importance of functions close to zero in MATLAB?

Functions close to zero are important in MATLAB because they are often used in mathematical and scientific computations. These functions can help to approximate the solutions to complex equations and can be used to optimize various parameters in a system. They are also useful for determining the stability and convergence of numerical algorithms.

Can functions close to zero cause errors in MATLAB?

Yes, functions close to zero can sometimes cause errors in MATLAB. This is because the computer may not be able to accurately represent extremely small numbers and may round them to zero. This can lead to incorrect calculations and results. To avoid this, it is important to use appropriate tolerance levels and to check for potential errors in your code.

Are there any tips for working with functions close to zero in MATLAB?

When working with functions close to zero in MATLAB, it is important to consider the accuracy and precision of your calculations. You can use the format function to adjust the display format of numbers in MATLAB and to view the exact values of small numbers. Additionally, it is helpful to use appropriate tolerance levels and to check for potential errors in your code to ensure accurate results.

Similar threads

Replies
1
Views
2K
Replies
10
Views
2K
Replies
8
Views
2K
Replies
2
Views
3K
Replies
3
Views
3K
Replies
3
Views
4K
Back
Top