Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Re: [中文 Scilab] 幫忙看一下,這個函數定義錯哪兒了?

87 views
Skip to first unread message

Yung-Jang Lee

unread,
Nov 7, 2012, 11:02:31 AM11/7/12
to ade-s...@googlegroups.com

function [x1, x2]=roots12(a, b, c)//<---- 傳回值 x1,x2
x1=%nan;x2=%nan;  //<--- 初始傳回值先設為無解 (%nan 代表 Not a number)
delta=b^2-4*a*c
if a==0    
disp("不是一元二次方程")
elseif a~=0 & delta<0
disp("方程无实根")
else x1=(-b+delta^(1/2))/(2*a),x2=(-b-delta^(1/2))/(2*a)
end
endfunction

[x1,x2]=roots12(1,1,1)

[x1,x2]=roots12(3,6,1)



要注意 :

    1. 函數傳回值在函數內一定要給值
       你給的函數
                 function y=roots12(a, b, c)
                        //  函數 裡面沒有給 y ,
                 endfunction
        傳回值沒計算, 且兩根沒有傳出來, 這樣不可以
   
    2 正常狀況要傳回兩實根 x1,x2 , 也要考慮無解時也需要傳回值 , 那麼用 %nan 當作無解時的傳回直
       %nan 是 scilab 內定之常數,代表 not a number


txwgzyzz <gansu...@gmail.com> 於 2012年11月7日下午10:10 寫道:
幫忙看一下,這個函數定義錯哪兒了?
總是運行出錯,在scilab5.3.3上運行的。
求幫忙啊
函數的目的是對任意一個二次函數a*x^2+b*x+c=0求根!

function
y=roots12(a, b, c)
delta=b^2-4*a*c
if a==0
disp("不是一元二次方程")
elseif a~=0 & delta<0
disp("方程无实根")
else x1=(-b+delta^(1/2))/(2*a),x2=(-b-delta^(1/2))/(2*a)
end
endfunction

--
您已訂閱「Google 網上論壇」的「中文 Scilab 教育論壇」群組,因此我們特別傳送這封郵件通知您。
如要在網路上查看這項討論,請造訪 https://groups.google.com/d/msg/ade-scilab/-/1eRn7gUVUbUJ
如要在此群組張貼留言,請傳送電子郵件至 ade-s...@googlegroups.com
如要取消訂閱此群組,請傳送電子郵件至 ade-scilab+...@googlegroups.com
如需更多選項,請造訪此群組:http://groups.google.com/group/ade-scilab?hl=zh-TW

Reply all
Reply to author
Forward
0 new messages