lapos
lapos

Reputation: 33

Find the smallest root of the equation in scilab

I need to find the smallest absolute root of the equation with an accuracy of 0.00001 using scilab. The equation itself: x - cos (1.04 * x) = 0. Need to build a graph to determine the interval at which the function changes sign. Then calculate the first and second derivatives. It is necessary to determine their sign, which should be the same for all of them (*).

If1, then the calculation is performed according to the following formulas1.

if2, then the calculation is performed according to the following formulas2.

The calculation ends when.

How can all this be implemented in scilab?

Ok, so I tried to do this in matlab. But I'm still not sure if everything is composed correctly, and how to transfer this to scilab?

clc;
clear;

syms x
f = x - cos(1.04*x);
a=0.5;
b=1;
eps=0.00001;
i=0;
c=(a+b)/2;
f1=diff(f);
f2=diff(f1);
while(abs(b-a)>eps)
    if((subs(f1,x,c)*subs(f2,x,c))>=0)
        a=a-(b-a)*subs(f,x,a)/(subs(f,x,b)-subs(f,x,a));
        b=b-subs(f,x,b)/subs(f1,x,b);
    else
        a=a-subs(f,x,a)/subs(f1,x,a);
        b=b-(b-a)*subs(f,x,b)/(subs(f,x,b)-subs(f,x,a));
    end
    i=i+1;
end
fprintf('b=% f \n', double(b))
ezplot(f,[0.5 1]),hold on
plot(b,subs(f,x,b),'or')
grid on

Here is what I have in scilab.

clc;
clear;
a=0.5;
b=1;
deff ("y = f (x)", "y = x-cos (1.04 * x)")
deff ("y = f1(x)", "y = 1.04.*sin(1.04*x)+1")
deff ("y = f2(x)", "y = 1.0816.*cos(1.04*x)")
eps=0.00001;
i=0;
c=(a+b)/2;
m=0;
com1 = ["k                 a                 b                 absolute          f(a)               f(b)              f1(b)"];
tab = [];
while(abs(b-a)>eps)
    if((f1(c)*f2(c))>=0)
        a=a-(b-a)*f(a)/(f(b)-f(a));
        b=b-f(b)/f1(b);
    else
        a=a-f(a)/f1(a);
        b=b-(b-a)*f(b)/(f(b)-f(a));
    end
    i=i+1;
    m=abs(b-a);
    tab = [tab; i a b m f(a) f(b) f1(b)];
end
disp('b=', double(b))
disp(tab)
fprintfMat("table.txt", tab, "%1.15f", com1)
fplot2d(-10:0.1:10,f)
plot(b,f(b),'or')
xgrid

Upvotes: 3

Views: 257

Answers (1)

Stéphane Mottelet
Stéphane Mottelet

Reputation: 3014

The equivalent command of Matlab ezplot does not exist but a close one is fplot2d. To add a grid you have xgrid.

    clc;
    clear;
    a=0.5;
    b=1;
    deff ("y = f (x)", "y = x-cos (1.04 * x)")
    deff ("y = f1(x)", "y = 1.04.*sin(1.04*x)+1")
    deff ("y = f2(x)", "y = 1.0816.*cos(1.04*x)")
    eps=0.00001;
    i=0;
    c=(a+b)/2;
    
    tab = [a,b];
    while(abs(b-a)>eps)
        if((f1(c)*f2(c))>=0)
            a=a-(b-a)*f(a)/(f(b)-f(a));
            b=b-f(b)/f1(b);
        else
            a=a-f(a)/f1(a);
            b=b-(b-a)*f(b)/(f(b)-f(a));
        end
        tab = [tab; a b];
        i=i+1;
    end
    disp('b=% f \n', double(b))
    disp(tab)
    fprintfMat("table.txt",tab)
    fplot2d(-10:0.1:10,f)
    plot(b,f(b),'or')
    xgrid

The image has been exported from Scilab with `xs2png(0,"graph.png") : enter image description here

Upvotes: 2

Related Questions