Have you seen anything like the super function code 'rszero' ?
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
Here is a code that does more than the 'fzero' and 'solve' commands.
_ Some of its advantages are;_
I) Unlike the command fzero, it is able to display a zero in complex number form (that is it will not display Nan if the value of the unknown is not real), and this could be helpful to a lot of mathematicians out there.
II) Also, it is able to display multiple outputs, that is multiple answers for the unknown.
III) Unlike the command fzero, it does not need a guessed value to get to the true value.
%code to evaluate the zero(es) of the input y, which is a function of x e.g x^2+4.
function []=rszero(y)
a=refiner(y,1+i); %refining 1+i
b=refiner(y,-1-i); %refining -1-i
c=refiner(y,-1+i); %refining -1+i
d=refiner(y,1-i); %refining 1-i
e=refiner(y,10+i); %refining 10+i
f=refiner(y,10-i); %refining 10-i
g=refiner(y,-10+i); %refining -10+i
h=refiner(y,-10-i); %refining -10-i
T=[a;b;c;d;e;f;g;h]; %arranging answers in array
x=unique(T) %removing repetitions
end
%creating a sub-function refiner that refines the super-zero to the true zero
function X=refiner(y,n)
syms x
X=n; %assigning X to the first super-zero
t=1; %assigning a value for t that is not = 0
dy_dx=diff(y); %differentiating y
while t~=0 %creating a loop for the iteration process
ys=subs(y,x,X); %evaluating y @x=X
if abs(ys)>=75
X=X/2; %this condition helps to reduce the no of iterations
t=1; %keeping t constant for this condition
else
dy_dxs=subs(dy_dx,x,X); %evaluating dy_dx @x=X
X=X-(ys/dy_dxs); %applying the refining equation
t=ys/dy_dxs; %changing the value of t
end
end
end
Examples
I) to find the value of x in the equation 14x - 8 =0, we compute on the command prompt rszero(14*x-8); it would display the answer 4/7
2) to find the value of x in the equation x2=4, we compute on the command prompt rszero(x^2-4); it should display the answer 2
3) to find the value of x in the equationequ 6x5+7x4+3x2+6x+4=0, we compute on the command prompt rszero(6*x^5+7*x^4+3*x^2+6*x+4), it should display the answers 1.2480, ...
4) to find the value of x in the equation 3x = 16x, we compute on the command prompt rszero(3^x-16*); it should display the answers 0.0673, 3.7194
5) to find the value of x in the equation exsinx = 5, we compute on the command prompt rszero(exp(x*sin(x))-5); it should display the answers -1.21-1.78I, ...
6 comentarios
Walter Roberson
el 8 de Feb. de 2020
Please post a formatted version of the code. Click on the '>' button in the CODE section of the editor here, then copy and paste your code.
Obasi Chukwuma
el 8 de Feb. de 2020
Obasi Chukwuma
el 9 de Feb. de 2020
Walter Roberson
el 10 de Feb. de 2020
With your given example x^3-5*x+6, that gets differentiated to 3*x^2 - 5, and you try to solve that for an exact 0 -- you keep executing while t~=0 . Your probe values such as 1+i are rational, not software floating point, so your process finds does calculations on rationals, and each step provides a refined rational output. But for 3*x^2 - 5 you cannot possibly find a rational value that gives an exact 0 output because the solution is irrational. Your algorithm will keep trying, slowing down as it deals with longer and longer software rationals. If left alone it would keep going to numbers below 10^-10000000 because the symbolic toolbox supports those... The software doesn't give out until 10^-68000000-something . That's a long execution time.
Obasi Chukwuma
el 10 de Feb. de 2020
Walter Roberson
el 10 de Feb. de 2020
You should probably check whether t is close to 0 instead of being exactly 0.
Respuestas (1)
Obasi Chukwuma
el 29 de Sept. de 2020
0 votos
La pregunta está cerrada.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!