Using project thinking to analyze problems and solve problems in MATLAB

Source: Internet
Author: User

We will think along the following steps:
1.State the problem
The problem with this example statement is simple. We want to write a procedure that determines the equation of two yuan: whether there are two real roots, repeating real roots, or two complex roots
2.Define the inputs and outputs
This procedure requires the input of two yuan of the first equation of three parameters: A,b,c
Determination of the output square Chenggen
3.Design the algorithm
A task can be divided into three parts: Input,processing,and output

Read the input data
Calculate The Roots
Write out the Roots

Now we divide the above three sections into smaller, finer modules.
According to the discriminant value, there are three things about the root, so it would be reasonable to execute the algorithm with a three-branch if structure.

The pseudo-code is:

Allow user to define three variables A,b,c
Read A,b,and C
discriminant type discriminant <-b^2-4*a*c
If discriminant >0
X1 <-(-B+SQRT (discriminant))/(2*a)
X2 <-(-b-sqrt (discriminant))/(2*a)
Write msg: Two Yuan the first equation has two unequal real roots!
Showing two of real roots
ElseIf discriminant = = 0
X1 <--b/(2*a)
Write msg: Two Yuan the first equation has two equal real roots!
Show this real roots
Else
Real_part <--b/(2*a)
Image_part <-sqrt (ABS (discriminant))/(2*a)
Write msg: Two Yuan the first equation has two complex roots
Show two complex roots
End
4.Turn the glgorithm into MATLAB statements
The final Matalab is as follows:

%script file: calc_roots.m% %Target:% The program solves the two-Yuan Equation group: a*x^2+ b*x + c =0 The root of the problem% %Release History%Date Editor Description%    =====         =========     ================% --Ten-2  -:TenBubble Source% %Define variables:% a--x^2 of Parameters% B--Parameters of x% c--Equation constant Parameter% discriminant--discriminant variable of equation% Imag_part--The imaginary root part of the equation% Real_part--Real Roots part of the equation% X1--equation Real Roots 1% X2--equation Real Roots 2% %clear variable or directive CLC;%allows the user to enter three parameters disp ('the function is to solve the root of the equation');d ISP ('equation: a*x^2 + b*x + C = 0.'); a= Input ('input parameter A:'); b= Input ('input parameter B:'); C= Input ('input parameter C:');%calculating discriminant type discriminant= b^2-4*a*C;%according to the discriminant type of discussionifdiscriminant >0%two x real roots x1= (-b+sqrt (discriminant))/(2*a) X2= (-b-sqrt (discriminant))/(2*a) disp ('two Yuan the first equation has two unequal real roots! '); fprintf ('x1=%f\n', X1); fprintf ('x2=%f\n', x2); ElseIf discriminant==0X1=-b/(2*a) disp ('two Yuan the first equation has two equal real roots! '); fprintf ('x1=x2=%f\n', X1);ElseReal_part=-b/(2*a) Image_part= sqrt (ABS (discriminant))/(2*a) disp ('two Yuan the first equation has two complex roots'); fprintf ('x1=%f +i%f\n', Real_part, Image_part); fprintf ('x2=%f-i%f\n', Real_part, Image_part); end

5.Test the program
The case of A B C root
1 5 6-2,-3
1 4 4-2
1 2 5-1+/-I2
Therefore, the algorithm gives the correct answers in three cases

Figures:

Using project thinking to analyze problems and solve problems in MATLAB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.