C PROGRAMMING HELP NEEDED! So, I've made progress on my script, but I'm having trouble making the next step in my assignment work properly. So far my script can: 1) Be prompted to type in coefficients of the Linear Systems and Two Initial Conditions. D2y+a1Dy+a2 = 0; and 2) Find the roots of the second order nontrivial solution. D2y+a1Dy+a2 = 0 => λ 2+a1 λ +a2 = 0 But I need it to do the following: Based on the roots λ1 and λ2 of the second order system, your script should decide: a) If roots (λ’s) are nonrepeating real , the response y(t) is y(t) = C1 e λ1t+C2e λ2t, for t≥0. Find C’s and display your y(t) on screen. b) If roots (λ’s) are repeating real (λ1 =λ2), the response y(t) is y(t) = (C1 +C2te)λt, for t≥0. Find C’s and display your y(t) on screen. c) If roots (λ’s) are complex conjugate (λ’s = α+jβ and α-jβ ), the response y(t) is y(t)=Cαtcos(βt+θ), for t≥0. Find C and θ and display your y(t) on screen. Please use conditionals. Also please use user-defined functions to compute C’s and θ. Also, Please, PLEASE NO IOSTREAM.H OR CACIO.H I CANNOT USE THOSE HEADERS. Thank you for your help! Pasted below is my script in its current state, I also attached a picture of the assingment so that it may be clearer: #include #include int main() { int a, b, c, d; double root1, root2; printf("Enter a, b and c where a*x*x + b*x + c = 0\n"); scanf("%d%d%d", &a, &b, &c); d = b*b - 4*a*c; if (d < 0) { //complex roots printf("First root = %.2lf + j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a)); printf("Second root = %.2lf - j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a)); } else { //real roots root1 = (-b + sqrt(d))/(2*a); root2 = (-b - sqrt(d))/(2*a); printf("First root = %.2lf\n",root1); printf("Second root = %.2lf\n",root2); } return 0; }
C
So, I've made progress on my script, but I'm having trouble making the next step in my assignment work properly.
So far my script can:
1) Be prompted to type in coefficients of the Linear Systems and Two Initial
Conditions.
D2y+a1Dy+a2 = 0;
and
2) Find the roots of the second order nontrivial solution.
D2y+a1Dy+a2 = 0 => λ 2+a1 λ +a2 = 0
But I need it to do the following:
Based on the roots λ1 and λ2 of the second order system, your script should decide:
a) If roots (λ’s) are nonrepeating real , the response y(t) is
y(t) = C1 e λ1t+C2e λ2t, for t≥0.
Find C’s and display your y(t) on screen.
b) If roots (λ’s) are repeating real (λ1 =λ2), the response y(t) is
y(t) = (C1 +C2te)λt, for t≥0.
Find C’s and display your y(t) on screen.
c) If roots (λ’s) are complex conjugate (λ’s = α+jβ and α-jβ ), the response y(t) is
y(t)=Cαtcos(βt+θ), for t≥0.
Find C and θ and display your y(t) on screen.
Please use conditionals. Also please use user-defined functions to compute C’s and θ. Also, Please, PLEASE NO IOSTREAM.H OR CACIO.H I CANNOT USE THOSE HEADERS.
Thank you for your help! Pasted below is my script in its current state, I also attached a picture of the assingment so that it may be clearer:
#include <stdio.h>
#include <math.h>
int main()
{
int a, b, c, d;
double root1, root2;
printf("Enter a, b and c where a*x*x + b*x + c = 0\n");
scanf("%d%d%d", &a, &b, &c);
d = b*b - 4*a*c;
if (d < 0) { //complex roots
printf("First root = %.2lf + j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second root = %.2lf - j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
}
else { //real roots
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
printf("First root = %.2lf\n",root1);
printf("Second root = %.2lf\n",root2);
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
Please create the header file as specified and implement it into my script. Thank you for your help, it is seriously appreciated! This is the last question I can post today so please answer thoroughly, I will upvote just as thoroughly!!
The header file deals with the following instruction prompt:
Find the roots of the second order nontrivial solution.
D2y+a1Dy+a2 = 0 => λ 2+a1 λ +a2 = 0
Create a header file for this step!! And this step Only!
I believe the portion of the script that needs to be turned into a utilized header file is located in the main() function. Thank you so much for your help, pasted is a copy of my script so far:
#include <stdio.h>
#include <math.h>
void compute_coefficients(double lambda1, double lambda2, double *C1, double *C2, double *C_alpha, double *theta);
void display_output(double lambda1, double lambda2, double C1, double C2, double C_alpha, double theta);
int main()
{
int a, b, c;
double lambda1, lambda2, C1, C2, C_alpha, theta;
printf("Enter a, b, and c where a*D2y + b*Dy + c*y = 0:\n");
scanf("%d%d%d", &a, &b, &c);
// Compute the roots of the characteristic equation
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) { // Complex roots
lambda1 = -b / (double)(2 * a);
lambda2 = sqrt(-discriminant) / (2 * a);
} else if (discriminant == 0) { // Repeated real roots
lambda1 = lambda2 = -b / (double)(2 * a);
} else { // Distinct real roots
lambda1 = (-b + sqrt(discriminant)) / (2 * a);
lambda2 = (-b - sqrt(discriminant)) / (2 * a);
}
// Compute coefficients and display output based on type of roots
compute_coefficients(lambda1, lambda2, &C1, &C2, &C_alpha, &theta);
display_output(lambda1, lambda2, C1, C2, C_alpha, theta);
return 0;
}
void compute_coefficients(double lambda1, double lambda2, double *C1, double *C2, double *C_alpha, double *theta)
{
if (lambda1 == lambda2) { // Repeated real roots
*C1 = 1;
*C2 = 0;
*C_alpha = lambda1;
*theta = 0;
} else if (fabs(lambda1 - lambda2) < 1e-10) { // Distinct real roots (with rounding tolerance)
*C1 = 1;
*C2 = lambda1;
*C_alpha = 0;
*theta = 0;
} else { // Complex conjugate roots
*C1 = 1;
*C2 = 0;
*C_alpha = sqrt(lambda1 * lambda1 + lambda2 * lambda2);
*theta = atan2(lambda2, lambda1);
}
}
void display_output(double lambda1, double lambda2, double C1, double C2, double C_alpha, double theta)
{
printf("The roots are:\n");
printf("lambda1 = %lf\n", lambda1);
printf("lambda2 = %lf\n", lambda2);
if (lambda1 == lambda2) { // Repeated real roots
printf("The solution is: y(t) = (C1 + C2 * t) * e^(%lf t)\n", lambda1);
printf("Please enter initial conditions y(0) and y'(0):\n");
double y0, y_prime0;
scanf("%lf %lf", &y0, &y_prime0);
C1= y0;
C2 = y_prime0 - lambda1 * y0;
printf("The solution is: y(t) = ( %lf + %lf * t ) * e^(%lf t)\n", C1, C2, lambda1);
} else if (fabs(lambda1 - lambda2) < 1e-10) { // Distinct real roots (with rounding tolerance)
printf("The solution is: y(t) = (C1 + C2 * t) * e^(%lf t)\n", lambda1);
printf("Please enter initial conditions y(0) and y'(0):\n");
double y0, y_prime0;
scanf("%lf %lf", &y0, &y_prime0);
C1 = y0;
C2 = y_prime0 - lambda1 * y0;
printf("The solution is: y(t) = ( %lf + %lf * t ) * e^(%lf t)\n", C1, C2, lambda1);
} else { // Complex conjugate roots
printf("The solution is: y(t) = e^(%lf t) * (C1 * cos(%lf t) + C2 * sin(%lf t))\n", C_alpha, theta, theta);
printf("Please enter initial conditions y(0) and y'(0):\n");
double y0, y_prime0;
scanf("%lf %lf", &y0, &y_prime0);
C1 = y0;
C2 = (y_prime0 - lambda1 * y0) / lambda2;
printf("The solution is: y(t) = e^(%lf t) * ( %lf * cos(%lf t) + %lf * sin(%lf t))\n", C_alpha, C1, theta, C2, theta);
}
}
Thank you for your help again! Attached is a copy of the question in the assignment as it may clarify things.
C
NO IOSTREAM.H OR CACIO.H PLEASE I CANT USE THOSE HEADERS.
Modify the given Script to meet the following conditions:
Create a C script that will:
1) Read a text file “Data.txt”. The numbers in the first column of Data.txt are a1’s, the
second column are a2’s, the third column are y(0)s and the fourth column are y’(0)s.
2) Use these numbers to find the Zero-Input Responses. (So, the user will not type in
those numbers. Your program should read those values from the Data.txt file automatically.
Then, your program should compute the roots and required coefficients.)
3) Save the result (Coefficients) on “Result.txt”
Here is the format for Result.txt;
1) The first column indicates whether the system has nonrepeating real roots, repeating
real roots, or complex conjugate roots. Use 1 for nonrepeating real roots, 2 for repeating
real roots, and 3 for complex conjugate roots.
2) The second column should be used for C1 or C’s (for repeating real roots or complex
conjugate)
3) The third column should be used for C2 or C (if it is repeating case) or Theta value
(for complex conjugate case)
Data.txt pasted:
6 9 3 -7
4 40 2 16.78
- 4 4 3 4
5 6 2 -1
4 13 5 15.98
#include <stdio.h>
#include <math.h>
void compute_coefficients(double lambda1, double lambda2, double *C1, double *C2, double *C_alpha, double *theta);
void display_output(double lambda1, double lambda2, double C1, double C2, double C_alpha, double theta);
int main()
{
int a, b, c;
double lambda1, lambda2, C1, C2, C_alpha, theta;
printf("Enter a, b, and c where a*D2y + b*Dy + c*y = 0:\n");
scanf("%d%d%d", &a, &b, &c);
// Compute the roots of the characteristic equation
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) { // Complex roots
lambda1 = -b / (double)(2 * a);
lambda2 = sqrt(-discriminant) / (2 * a);
} else if (discriminant == 0) { // Repeated real roots
lambda1 = lambda2 = -b / (double)(2 * a);
} else { // Distinct real roots
lambda1 = (-b + sqrt(discriminant)) / (2 * a);
lambda2 = (-b - sqrt(discriminant)) / (2 * a);
}
// Compute coefficients and display output based on type of roots
compute_coefficients(lambda1, lambda2, &C1, &C2, &C_alpha, &theta);
display_output(lambda1, lambda2, C1, C2, C_alpha, theta);
return 0;
}
void compute_coefficients(double lambda1, double lambda2, double *C1, double *C2, double *C_alpha, double *theta)
{
if (lambda1 == lambda2) { // Repeated real roots
*C1 = 1;
*C2 = 0;
*C_alpha = lambda1;
*theta = 0;
} else if (fabs(lambda1 - lambda2) < 1e-10) { // Distinct real roots (with rounding tolerance)
*C1 = 1;
*C2 = lambda1;
*C_alpha = 0;
*theta = 0;
} else { // Complex conjugate roots
*C1 = 1;
*C2 = 0;
*C_alpha = sqrt(lambda1 * lambda1 + lambda2 * lambda2);
*theta = atan2(lambda2, lambda1);
}
}
void display_output(double lambda1, double lambda2, double C1, double C2, double C_alpha, double theta)
{
printf("The roots are:\n");
printf("lambda1 = %lf\n", lambda1);
printf("lambda2 = %lf\n", lambda2);
if (lambda1 == lambda2) { // Repeated real roots
printf("The solution is: y(t) = (C1 + C2 * t) * e^(%lf t)\n", lambda1);
printf("Please enter initial conditions y(0) and y'(0):\n");
double y0, y_prime0;
scanf("%lf %lf", &y0, &y_prime0);
C1= y0;
C2 = y_prime0 - lambda1 * y0;
printf("The solution is: y(t) = ( %lf + %lf * t ) * e^(%lf t)\n", C1, C2, lambda1);
} else if (fabs(lambda1 - lambda2) < 1e-10) { // Distinct real roots (with rounding tolerance)
printf("The solution is: y(t) = (C1 + C2 * t) * e^(%lf t)\n", lambda1);
printf("Please enter initial conditions y(0) and y'(0):\n");
double y0, y_prime0;
scanf("%lf %lf", &y0, &y_prime0);
C1 = y0;
C2 = y_prime0 - lambda1 * y0;
printf("The solution is: y(t) = ( %lf + %lf * t ) * e^(%lf t)\n", C1, C2, lambda1);
} else { // Complex conjugate roots
printf("The solution is: y(t) = e^(%lf t) * (C1 * cos(%lf t) + C2 * sin(%lf t))\n", C_alpha, theta, theta);
printf("Please enter initial conditions y(0) and y'(0):\n");
double y0, y_prime0;
scanf("%lf %lf", &y0, &y_prime0);
C1 = y0;
C2 = (y_prime0 - lambda1 * y0) / lambda2;
printf("The solution is: y(t) = e^(%lf t) * ( %lf * cos(%lf t) + %lf * sin(%lf t))\n", C_alpha, C1, theta, C2, theta);
}
}
I'm gonna attach a screenshot of the assignment to clarify any smudgy details. Thanks again!