Aproximate roots using Bairstow method

2 visualizzazioni (ultimi 30 giorni)
MARCU DIANA-NICOLETA
MARCU DIANA-NICOLETA il 2 Giu 2020
Risposto: Sai Sri Pathuri il 9 Lug 2020
Hello guys.
I have a code in c++, which implement the Bairstow method, but I don't know how to convert it in Matlab.
Can anyone help me with some tips ? I know it is a code on this webside, which makes the same thing, but I don't understand it very well.
This is the code :
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
double a[100], p0, q0, b[100], c[100], delta, P, Q, R, S, eps;
int n, i;
double max(double a, double b) {
if (a >= b)
return a;
else
return b;
}
void rezolv_ec2(double a, double b, double c) {
double d, x1, x2;
d = b * b - 4 * a*c;
if (d >= 0) {
cout << "\n" << (-b - sqrt(d)) / (2 - a) << endl;
cout << "\n" << (-b + sqrt(d)) / (2 - a) << endl;
}
if (d < 0) {
cout << "\n" << -b / (2 * a) << "-i*" << sqrt(-d) / (2 * a);
cout << "\n" << -b / (2 * a) << "+i*" << sqrt(-d) / (2 * a);
}
}
void rezolv_ec1(double a, double b) {
cout << "\n" << -b / a << endl;
}
void main() {
cout << "Dati gradul ec.="; cin >> n;
for (i = 0; i <= n; i++) {
cout << "a[" << i << "]="; cin >> a[i];
}
do {
cout << "\np0="; cin >> p0;
cout << "\nq0="; cin >> q0;
cout << "\nDati eroarea admisa eps="; cin >> eps;
do {
b[0] = a[0];
b[1] = a[1] - p0 * b[0];
for (i = 2; i <= n; i++) {
b[i] = a[i] - p0 * b[i - 1] - q0 * b[i - 2];
}
c[0] = b[0];
c[1] = b[1] - p0 * c[0];
for (i = 2; i <= n - 1; i++) {
c[i] = b[i] - p0 * c[i - 1] - q0 * c[i - 2];
}
delta = pow(c[n - 2], 2) - c[n - 3] * c[n - 1] + c[n - 3] * b[n - 1];
P = -b[n - 1] * c[n - 2] + b[n] * c[n - 3];
Q = -b[n] * c[n - 2] + b[n - 1] * c[n - 1] - pow(b[n - 1], 2);
p0 -= P / delta;
q0 -= Q / delta;
R = b[n - 1];
S = b[n] + p0 * b[n - 1];
} while (max(fabs(R), fabs(S)) > eps);
cout << "\nRadacinile ecuatiei sunt:";
rezolv_ec2(1, p0, q0);
n = n - 2;
for (i = 0; i <= n; i++) {
a[i] = b[i];
}
} while (n >= 3);
if (n == 2)
rezolv_ec2(b[0], b[1], b[2]);
if (n == 1)
rezolv_ec1(b[0], b[1]);
}

Risposte (1)

Sai Sri Pathuri
Sai Sri Pathuri il 9 Lug 2020
You may use the following resources
  1. Declare function name, inputs, and outputs - https://in.mathworks.com/help/matlab/ref/function.html
  2. if, elseif, else Documentation - https://in.mathworks.com/help/matlab/ref/if.html
  3. Display text in the Command Window - disp or fprintf
  4. Loops - for and while
  5. Do while loop in MATLAB - https://in.mathworks.com/matlabcentral/answers/115403-do-while-loop-in-matlab#answer_123751

Categorie

Scopri di più su Code Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by