%CN_LAB4_ES1 script che disegna funzioni in un dato intervallo % % restituisce il grafico della funzione f(x) % e di f1(x) e f2(x) (f(x) = f1(x)-f2(x)) % per x in [a,b] % Disegnamo le 4 funzioni di input clf clear all clc % funzione 1 % a = -0.5; % b = 1; % % f = @(x) sqrt(x+1) -exp(-x); % f1 =@(x) x; % f2 =@(x) exp(-2.*x)-1; % funzione 2 % a= -2; % b= 2; % % f=@(x) exp(x) -5 + x.^2; % f1 =@(x) x; % f2 =@(x) log(5 - x.^2); % % funzione 3 % a= -1; % b= 1; % % f=@(x) 3*x - cos(x); % f1 =@(x) x; % f2 =@(x) cos(x)/3; % % funzione 4 a= 0.5; b= 1; f=@(x) x + log(x); % % f(0) %-inf f1 =@(x) x; f2 =@(x) -log(x); % %in alternativa chiediamo i dati come input all'utente % a = input('Estremo sinistro dell''intervallo a = '); % b = input('Estremo destro dell''intervallo b = '); % f = input('funzione (handle function) f = '); % f1 = input('handle function f1 = '); % f2 = input('handle function f2 = '); x = linspace(a,b); figure(1) plot(x, f(x), 'b--','Linewidth', 2) axis([a b -5 5]) hold on plot([a b],[ 0 0], ':k',[0 0], [-5 5],':k','Linewidth', 2) %plot the x axis hold off title(['f(x) = 0 ' func2str(f) ]) legend('y = f(x)') figure(2) %f1(x) %f2(x) plot(x, f1(x), x, f2(x),'Linewidth', 2) hold on plot([a b],[ 0 0], ':k',[0 0], [-5 5],':k', 'Linewidth', 2) %plot the x axis hold off title(['x = g(x) x=' func2str(f2) ]) legend('y = x', 'y = g(x)') axis([a b -5 5])