How can i include repeating and complex roots within my code ?

16 次查看(过去 30 天)
Displayed below is a set code that is meant to solve linear second order homogeneous differential equations where the user is prompted to include 3 coefficients and two intial conditions. I have been tasked to "complete" this code by allowing the roots to be complex and repeated as solutions generated originate from 2 real distinct roots. Any support into how i can alter this code to fit these requirements are greatly appreciated.
c1='Please enter a value for coefficient 1 ';
a=input(c1);
c2='Please enter a value for coefficient 2 ';
b=input(c2);
c3='Please enter a value for coefficient 3 ';
c=input(c3);
intial1='Enter initial condition for the solution' ;
ytime0=input(intial1);
intial2='Enter initial condition of the derivative of solution' ;
ydtime0=input(intial2);
fprintf('The values entered: \n a = %d \n b = %d \n c = %d\n', a,b,c);
response=(b.^2)-(4*a*c);
disp(response)
if(response>0)
clc();
disp('The response is over-damped')
m1=(-b+sqrt(response))./(2*a);
m2=(-b-sqrt(response))./(2*a);
fprintf(' The roots of the characteristic equation \n m1 = %d \n m2 = %d \n', m1, m2);
D=[1 1;m1 m2];
F=[ytime0;ydtime0];
sol=inv(D)*F;
cons1=sol(1);
cons2=sol(2);
if (m1>m2)
tau=1/m1;
fprintf('the value is %d\n',tau)
end
if (m1<m2)
tau=1/m2;
fprintf('the value is %d\n',tau)
end
time5=7*abs(tau);
disp(time5)
t=linspace(0,time5);
y=cons1*exp(m1*t)+cons2*exp(m2*t);
plot(t,y)
end
if(response==0)
disp('The response is critically-damped')
end
if(response<0)
disp('The response is underdamped')
end

采纳的回答

Walter Roberson
Walter Roberson 2021-8-15
编辑:Walter Roberson 2021-8-15
m1 = randn() + 1i*randn();
m2 = randn() + 1i*randn();
fprintf(' The roots of the characteristic equation \n m1 = %d \n m2 = %d \n', m1, m2);
The roots of the characteristic equation m1 = -1.235814e+00 m2 = -6.350351e-01
The %d format should only be used for integers that have no imaginary part.
MATLAB does not have any % format item for complex numbers: you have to break them into real and imaginary part. However, you can take shortcuts such as
fprintf(' The roots of the characteristic equation \n m1 = %s \n m2 = %s \n', string(m1), string(m2));
The roots of the characteristic equation m1 = -1.2358+0.74685i m2 = -0.63504-0.72677i

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by