Solving A QuadraticEquation by Java

Purpose:
	This program solves for the roots
	of a quadratic equation of the form
	a*x*x+b*x+c=0. It calculates the
	answers regardless of the type of
	roots that the equation possesses.


import java.io.*;
public class QuadraticEquation
{
	//Define the main method.
	public static void main(String[] args) throws IOException
	{
		//Declare variables,and define each variable.
		double a;
		double b;
		double c;
		double discriminant;
		double imag_part;
		double real_part;
		double x1;
		double x2;

		//Create a buffered reader.
		BufferedReader inl=new BufferedReader(new InputStreamReader(System.in));

		//Prompt the user for the coefficients of the equation.
		System.out.println("This program solves for the roots of quadratic equation.");
		System.out.println("Please input the coefficients of quadratic equation:");

		System.out.print("Enter the coefficients A: ");
		a=Double.parseDouble(inl.readLine());

		System.out.print("Enter the coefficients B: ");
		b=Double.parseDouble(inl.readLine());

		System.out.print("Enter the coefficients C: ");
		c=Double.parseDouble(inl.readLine());

		System.out.println("The quadratic equation you inputed is:");
		System.out.println(a+"*x*x + "+b+"*x + "+c+" = 0");

		//Caluate discriminant
		discriminant=b*b-4*a*c;

		//Solve for the roots,depending on the discriminant.
		if(discriminant>0)
		{
			//Two real roots
			x1=(-b+Math.sqrt(discriminant))/(2*a);
			x2=(-b-Math.sqrt(discriminant))/(2*a);
			System.out.println("This equation has two real roots:");
			System.out.println("X1 = "+x1+" , X2 = "+x2);
		}
		else
			if(discriminant==0)
			{
				//One repeated root
				x1=(-b)/(2*a);
				System.out.println("This equation has repeated real roots:");
				System.out.println("X1 = X2 = "+x1);
			}
			else
			{
				//Two complex root
				real_part=(-b)/(2*a);
				imag_part=Math.sqrt(Math.abs(discriminant))/(2*a);
				System.out.println("This equation has two Complex roots:");
				System.out.println("X1 = "+real_part+"+"+imag_part+"i");
				System.out.println("X2 = "+real_part+"-"+imag_part+"i");
			}
	}
}


Sure, here is an example code for solving the 1D compressible Euler equation in Matlab with a shock happening: ```matlab % Define the domain and initial conditions x = linspace(0,1,1000); rhoL = 1; rhoR = 0.125; uL = 0; uR = 0; pL = 1; pR = 0.1; gamma = 1.4; x0 = 0.5; tEnd = 0.25; dx = x(2)-x(1); dt = 0.01; % Initialize the solution arrays rho = ones(size(x))*rhoL; u = ones(size(x))*uL; p = ones(size(x))*pL; % Set up the shock initial condition for i = 1:length(x) if x(i) >= x0 rho(i) = rhoR; u(i) = uR; p(i) = pR; end end % Define the flux function flux = @(rho,u,p) [rho.*u; rho.*u.^2+p; u.*(gamma*p./(gamma-1)+0.5*rho.*u.^2+u.^2)]; % Solve the Euler equations with an explicit upwind scheme t = 0; while t < tEnd % Compute the time step lambda = max(abs(u) + sqrt(gamma*p./rho)); dt = dx / lambda; % Compute the fluxes at the cell interfaces F = flux(rho,u,p); Fp = F(:,2:end); Fm = F(:,1:end-1); % Compute the numerical flux using the upwind scheme Fnum = 0.5*(Fp+Fm - lambda.*(rho(:,2:end)-rho(:,1:end-1))); % Update the solution rho(:,2:end-1) = rho(:,2:end-1) - dt/dx*(Fnum(1,2:end)-Fnum(1,1:end-1)); u(:,2:end-1) = u(:,2:end-1) - dt/dx*(Fnum(2,2:end)-Fnum(2,1:end-1)); p(:,2:end-1) = p(:,2:end-1) - dt/dx*(Fnum(3,2:end)-Fnum(3,1:end-1)); % Apply boundary conditions rho(:,1) = rho(:,2); u(:,1) = u(:,2); p(:,1) = p(:,2); rho(:,end) = rho(:,end-1); u(:,end) = u(:,end-1); p(:,end) = p(:,end-1); % Update the time t = t + dt; end % Plot the final solution plot(x,rho,'-k',x,u,'-r',x,p,'-b') xlabel('x') ylabel('Density, Velocity, Pressure') legend('Density','Velocity','Pressure') title('1D Compressible Euler Equation with Shock') ``` Note that this code uses an explicit upwind scheme to solve the equations, which may not be stable for all choices of parameters. If you encounter stability issues, you may need to use a different numerical method. Additionally, this code assumes a constant gamma value of 1.4; if you need to use a different value of gamma, you will need to modify the code accordingly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值