Buscar

COMANDOS SCILAB

Prévia do material em texto

//Método de Euler 
function 
 clear 
 clc 
 clf reset 
endfunction 
function [x, y]=Euler(a, b, h, y0)// a= valor inicial 
de x 
 // b= valor final de x 
 // h= espaçamento 
 // y0= valor da função 
 function z=df(x, y) 
 z = x + y 
 endfunction 
 c 
 x = [a: h :b] // Coordenadas do eixo espaçadas 
pela altura 
 y(1) = y0 // Coordenada inicial de y0 
 n = length(x) // Quantidade de pontos 
 for i = 2 : n 
 y(i)=y(i-1)+h*df(x(i-1),y(i-1)) 
 end 
 disp(y, "Respostas") // Exibe os valores de y das 
iterações dadas 
 plot2d4(x,y), xlabel('x'), ylabel('y') // Plota o 
gráfico da função 
endfunction 
//Estruturas de repetição 
clc 
clear 
n=input('Entre com o valor de n:'); 
Soma=0; 
for i=1:2:2*n-1; 
 Soma=Soma+i; 
end 
disp([Soma n^2 n+1 Soma^3 ]) 
//Estruturas de condição e repetição 
clc 
clear 
s=0; 
x=1; 
for i=1:99 
 s=x/2*i-1; 
 printf("s=%d\n",s) 
 end 
//Método de solução de EDO's 
clc 
clear 
clf reset 
//Dados da Função 
//Exata 
function [x, y]=EX(a, b, h, y0) 
 x= a:h:b 
 x0=a; // tempo inicial 
 y=ode(y0,x0,x,df); 
endfunction 
 
function z=df(x, y) 
 z=-1.2*y+7*exp(-0.3*x); 
endfunction 
//Método de Euler 
function [x, y]=Euler(a, b, h, y0) 
 x=a:h:b 
 n=length(x) 
 y(1)=y0 
 for i=1:n-1 
 y(i+1)=y(i)+df(x(i),y(i))*h 
 end 
endfunction 
 
//Método de Heun 
function [x, y]=Heun(a, b, h, y0) 
 x=a:h:b 
 n=length(x) 
 y(1)=y0 
 for i=1:n-1 
 f1=df(x(i),y(i)) 
 f2=df(x(i+1),y(i)+f1*h) 
 f=0.5*(f1+f2) 
 y(i+1)=y(i)+f*h 
 end 
endfunction 
 
//Método RK4 
function [x, y]=rk4(a, b, h, y0) 
 x=a:h:b 
 n=length(x) 
 y(1)=y0 
 for i=1:n-1 
 f1=df(x(i),y(i)) 
 f2=df(x(i)+h/2,y(i)+f1*h/2) 
 f3=df(x(i)+h/2,y(i)+f2*h/2) 
 f4=df(x(i)+h,y(i)+f3*h) 
 f=(f1+2*f2+2*f3+f4)/6 
 y(i+1)=y(i)+f*h 
 end 
endfunction 
 
//Valores 
[x,ye]= Euler(0,2.5,0.5,3) 
[x,yh]= Heun(0,2.5,0.5,3) 
[x,yrk4]= rk4(0,2.5,0.5,3) 
[x,ex]=EX(0,2.5,0.5,3) 
 
//Gráficos 
 
plot(x,ye,'g', x,yh,'r',x,yrk4,'b',x,ex,'k') 
legend('Euler','Heun','RK4','Exata') 
//Estruturas de condição e repetição 
clc 
clear 
a=0; 
for i=1:150 
 a=a+1; 
 printf("a=%d\n",a) 
 end 
 
//FUNCTION 
function [fexato]=f(x, y) 
fexato=(x-2) 
endfunction 
//Método_Newton_Rapson 
clc 
clear 
//f(x)=x*x-2 
N=input('Entre com o valor máximo de 
iterações:'); //número máx de iterações 
x0=1; //aproximação inicial 
erro=10^(-10); //erro 
xn=x0; 
for n=1:N 
 xn1= xn-(xn*xn-2)/(2*xn); 
 if abs((xn1-xn)/xn1) < erro then 
 printf('Valor da raiz é: %10.20f', xn1) 
 return 
 end 
 xn = xn1 
end 
printf('Não converge em n=%f iterações',N) 
 
//Estrutura do comando while 
 
clc 
clear 
 
n=0; 
Epsilon=1; 
while 1 + Epsilon > 1 
 n = n + 1; 
 Epsilon = Epsilon / 2; 
end 
disp([n Epsilon]) 
 
//aproximação de um número irracional 
 
clc 
clear 
 
while %T 
 numero = input('Entre com o valor do 
número:'); 
 if numero <=0 
 break 
 end 
 tolerancia = input('Entre com o valor da 
tolerância:'); 
 [num,den]=rat(numero,tolerancia); 
 disp([numero num den num/den numero-
num/den]) 
end

Continue navegando