finding roots in a quadratic formula

15 ビュー (過去 30 日間)
STUDENT
STUDENT 2019 年 11 月 30 日
コメント済み: Vladimir Sovkov 2019 年 11 月 30 日
im not entirely sure what i am doing wrong but this is the sciprt that i have written so far
function (x1, x2) = quad(a, b, c);
z= sqrt(b^2 -4*a*c);
w= 2*a;
x1= (-b+z)/w;
x2=(-b-z)/w;
a= input('Enter a Value for a')
b= input('Enter a value for b')
c= input('enter a vlaue for c')
if z<<0
fprintf('There are no real values')
else if z=0
fprintf('There is only one real root')
else z>>0
fprintf('There is two real roots')
end
  1 件のコメント
Vladimir Sovkov
Vladimir Sovkov 2019 年 11 月 30 日
(1) The discriminant is z=b^2 -4*a*c (no sqrt) - its sign matters;
(2) must be: line 9 ...z<0, line 11 ...z==0, line 13 z>0;
(3) if you transfer a, b, c as the function arguments, why do you use the "input" commands for them later? Their values renewed with "input" are never used.

サインインしてコメントする。

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 30 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 30 日
function [z,x1,x2]=quad(a,b,c)
z=sqrt(b^2 -4*a*c);
w=2*a;
x1=(-b+z)/w;
x2=(-b-z)/w;
end
Call the quad function in Main Script:
a=input('Enter a Value for a: ');
b=input('\nEnter a value for b: ');
c=input('\nEnter a vlaue for c: ');
[z,x1,x2]=quad(a,b,c);
if z<0
fprintf('There are no real values')
elseif z==0
fprintf('There is only one real root')
else
fprintf('There is two real roots')
end
  2 件のコメント
STUDENT
STUDENT 2019 年 11 月 30 日
the program wont run it says
Error: File: quad.m Line: 9 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "quad".)
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 30 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 30 日
Save the function file in another Matlab file named as quad.m and call that function from main script, it OK (no coding error).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePlatform and License についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by