撰寫者:陳姿穎
----------------------------------------------------------------------------------------------------
pro eqx
; This program want to calculate the solutions Quadratic Equations with Two Variables.
; ax^2 + bx + c = 0 with given costants a, b and c
; read a, b, c from the file, T1.txt
; the solutions, x1 and x2, will be print to the file, Sol_01.txt
; written by Ann Chen, final revised on May 20, 2010.
close,1
close,2
openr,1,"D:\IDL_learn\T1.txt"
openw,2,"D:\IDL_learn\Sol_01.txt"
aa=' '
ap=' + '
am=' - '
ai='i'
readf,1,aa
readf,1,aa
printf,2,' a b c x1 x2'
printf,2,'---------------------------------------------------'
WHILE (not(eof(1))) DO BEGIN
readf,1,format='(3(x,i4))',a,b,c
delta=b^2-4.*a*c
if delta ge 0 then begin
delta2=sqrt(delta)
x1=(-b+delta2)/(2.*a)
x2=(-b-delta2)/(2.*a)
printf,2,format='(3(x,i4),2(8x,f6.2))',a,b,c,x1,x2
endif else begin
xi=sqrt(-delta)/(2.*a)
xr=-b/(2.*a)
printf,2,format='(3(x,i4),2(x,f6.2,a,f6.2,a))',a,b,c,xr,ap,xi,ai,xr,am,xi,ai
endelse
endwhile
;aa=' '
;readf,1,aa ;readf > Read from file To skip the 1st line
;readf,1,aa ;To skip the 2nd line
close,1
close,2
print,'ok'
end
--------------------------------------------------
pro eqx_plot
; This program want to plot the solutions Quadratic Equations with Two Variables.
; ax^2 + bx + c = 0 with given costants a, b and c
; read a, b, c from the file, T1.txt
; the plot to ps file
; written by Ann Chen, final revised on May 20, 2010.
openr,1,"D:\IDL_learn\T1.txt"
n=2000
x=fltarr(n)
y=fltarr(n)
aa=' '
readf,1,aa
readf,1,aa
readf,1,format='(3(x,i4))',a,b,c
for i=0,n-1 do begin
x(i)=-1000+i
y(i)=a*x(i)^2+b*x(i)+c
endfor
set_plot,'ps'
device,filename='D:\IDL_learn\eqx_plot.ps'
plot,x,y,title='y(x)',xtitle='x',ytitle='y',$
xrange=[-10,10],yrange=[-10,200]
device,/close
close,1
close,2
print,'ok'
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
T1:
a b c
------------------------
1 -2 -8
Sol_01:
a b c x1 x2
---------------------------------------------------
1 -2 -8 4.00 -2.00
留言列表