Sir you are right.

3 views (last 30 days)

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, ...

Opportunities for recent engineering grads.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_794881

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_794881

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_794888

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_794888

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795339

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795339

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795346

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795346

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795348

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795348

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795365

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/504306-have-you-seen-anything-like-the-super-function-code-rszero#comment_795365

Sign in to comment.