Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.4 kB
1
Indexable
Never
clear;
clc;

// zad 1
r = 4
n_max = 50
a = 0.5
b = 2
h = 0.001

// zad 2
function y=f(x);
    y = (x^2 - 3)*sin(x)
endfunction


// zad 3
function y=fp(x,h);
    y = (f(x+h) - f(x))/h
endfunction

function y=fpp(x,h);
    y = (fp(x+h,h) - fp(x))/h
endfunction

kod = 0
wi=0
// zad 

if f(a) * f(b) < 0 then
    wi= 1
    x(wi) = a
    while ~(fp(a,h)*fp(b,h) > 0 && fpp(a,h) * fpp(b,h) > 0)
        if ~(wi<n_max) then
            kod = 1
            break;
        end
        
        wi = wi + 1;
        x(wi) = (a+b)/2;
        
        if abs(x(wi) - x(wi-1)) < 10^(-r) then
            kod = 2;
            break;
        end;
        
        if f(a) * f(x(wi)) < 0 then
            b = x(wi);
        else
            a = x(wi);
        end
    end
    
    if kod == 0 then
        // metoda r-f.
        wi = wi + 1;
    
        if fp(a,h) * fpp(a,h) < 0 then
            c = a
            x(wi) = b
        else
            c = b
            x(wi) = a
        end
    
        while(wi < n_max)
            wi = wi + 1
            x(wi) = x(wi-1) - f(x(wi-1)) * (c-x(wi-1))/(f(c) - f(x(wi-1)))
        
            if abs(x(wi) - x(wi-1)) < 10^(-r) then
                kod = 2;
                break;
            end
        end
    end
else
    kod = 3
end
Leave a Comment