필터 지우기
필터 지우기

Quadratic question using the function command

조회 수: 65 (최근 30일)
Chris
Chris 2013년 6월 25일
댓글: Walter Roberson 2019년 12월 27일
Without using the roots command, write a function to calculate roots of the quadratic equation
ax^2+bx+c
Inputs to the function should be the coefficients a, b and c and output should
be the roots. Test your program by setting a=2, b=3 and c= -1
So I know i have to have two files. One with the quadratic equations and the other script recalls the function. Heres What I have so far.
function x=quad_equation(a,b,c)
x(1)=(-b+sqrt(b.^2-4.*a.*c))/(2.*a)
x(2)=(-b-sqrt(b.^2-4.*a.*c))/(2.*a)
Thats the first file
a=3;
b=3;
c=-1;
function outputs=quad_equation(a,b,c)
output=fzero(
And I am lost on what to do with the second file

채택된 답변

Walter Roberson
Walter Roberson 2013년 6월 25일
In MATLAB, files that define functions must have "function" as the first non-comment word in the file; if you put assignments before the "function" statement then you will get an error about function definitions not permitted in that context.
  댓글 수: 2
Chris
Chris 2013년 6월 25일
function fx=quad_function(a,b,c)
a=3
b=2
c=-1
a_root=fzero(quad_function,a)
b_root=fzero(fx,b)
c_root=fzero(fx,c)
Alrght so I got this but how do I find the roots of the function?
Walter Roberson
Walter Roberson 2019년 12월 27일
function fx = quad_function
a=3;
b=2;
c=-1;
fx = quad_equation(a, b, c);
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

abhirup chowdhury
abhirup chowdhury 2019년 12월 27일
function fx=quad_function(a,b,c)
a=3
b=2
c=-1
a_root=fzero(quad_function,a)
b_root=fzero(fx,b)
c_root=fzero(fx,c)
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 12월 27일
Yes? That duplicates what Chris posted in 2013 ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Quadratic Programming and Cone Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by