MATLAB Answers

Have you seen anything like the super function code 'rszero' ?

3 views (last 30 days)
Obasi Chukwuma
Obasi Chukwuma on 8 Feb 2020
Answered: Obasi Chukwuma on 29 Sep 2020
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 Comments

Show 3 older comments
Walter Roberson
Walter Roberson on 10 Feb 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
Obasi Chukwuma on 10 Feb 2020
OK sir, I will work on this and give you a reply soon, it was meant to refine the value 1+i to the true zero of that equation, it's still a work in progress but some adjustment will to cut the long execution time. If you compute for an equation like 3*x-5, it will in no time pop up the answer in no time, that is 5/3. I think we can tackle this lagging problem with some tweaking or adjustment in the code, I'll see to that. Thanks for your contribution
Walter Roberson
Walter Roberson on 10 Feb 2020
You should probably check whether t is close to 0 instead of being exactly 0.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!