Advertisement
Adytzu04

matlab

Dec 15th, 2012
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.91 KB | None | 0 0
  1. EPS=input('EPS=');
  2. xi=input('xi=');
  3.     xf=input('xf=');
  4.  
  5. while xi>=xf
  6.     xi=input('xi=');
  7.     xf=input('xf=');
  8. end
  9.  
  10.  he=input('he=');
  11. while (he<0 || xi+he >=xf)
  12.     he=input('he=');
  13. end
  14.  
  15. xe=xi:he:xf;
  16. np=length(xe);
  17.  
  18. if(xe(np)<xf)
  19.     xe=[xe xf];
  20.     np=np+1;
  21. end
  22.  
  23. ye=FCE(xe);
  24.  
  25. plot(xe,ye,'-r');
  26.  
  27. xlabel('axa x');
  28. ylabel('axa y');
  29. Title('LABORATOR CN');
  30. grid;
  31.  
  32. x0=input('xo=');
  33. x1=input('x1=');
  34. x2=input('x2=');
  35.  
  36. f0=FCE(x0);
  37. f1=FCE(x1);
  38. f2=FCE(x2);
  39.  
  40. iter=0;
  41. xr=x2;
  42. fr=f2;
  43.  
  44. while (abs(fr)>EPS)
  45.     iter=iter+1;
  46.     if(x2>x0)
  47.         t=x2;
  48.         x2=x0;
  49.         x0=t;
  50.         t=f2;
  51.         f2=f0;
  52.         f0=t;
  53.     end
  54.     if (x0>x1)
  55.         t=x0;
  56.         x0=x1;
  57.         x1=t;
  58.         t=f0;
  59.         f0=f1;
  60.         f1=t;
  61.     end
  62.     if(x2>x1)
  63.         t=x2;
  64.         x2=x1;
  65.         x1=t;
  66.         t=f2;
  67.         f2=f1;
  68.         f1=t;
  69.     end
  70.     h1=x1-x0;
  71.     if(abs(h1)<EPS)
  72.         fprintf('h1-nul:algoritmul nu converge');
  73.         return;
  74.     end
  75.    
  76.     h2=x0-x2;
  77.     r=h2/h1;
  78.     d=r*(r+1)*(h1^2);
  79.     if(abs(d)<EPS)
  80.         fprintf('numitor nul pentru a');
  81.         return;
  82.     end
  83.     a=(r*f1-f0*(1+r)+f2)/d;
  84.     b=(f1-f0-a*(h1^2))/h1;
  85.     c=f0;
  86.     delta=b^2-4*a*c;
  87.     if(delta<=0)
  88.         fprintf('radacini complexe sau confundate');
  89.         return;
  90.     end
  91.    
  92.     if(b>=0)
  93.         d=b+sqrt(delta);
  94.     else
  95.         d=b-sqrt(delta);
  96.     end
  97.    
  98.     if(abs(d)<EPS)
  99.         fprintf('numitor nul pentru xr');
  100.         return;
  101.     end
  102.    
  103.     xr=x0-(2*c)/d;
  104.     fr=FCE(xr);
  105.     if(xr>x0)
  106.         x1=xr;
  107.         f1=fr;
  108.     else
  109.         x2=xr;
  110.         f2=fr;
  111.     end
  112.     fprintf('\niter=%g',iter);
  113.    
  114.      fprintf('\nxr=%g',xr);
  115.      
  116.       fprintf('\nfr=%g',fr);
  117.      
  118.   end
  119.  
  120.    fprintf('\nnumar total iteratii=%g',iter);
  121.    
  122.     fprintf('\nsolutie xr=%g',xr);
  123.    
  124.      fprintf('\nvaloare functie fr=%g',fr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement