0% found this document useful (0 votes)
5 views3 pages

To Provide The

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

To Provide The

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

To provide the MATLAB code for solving linear and nonlinear equations with a single variable

x and plotting these equations, I will also include a set of 5 equations with 5 unknowns.

Step 1: MATLAB Code for Plotting Linear and Nonlinear Equations (Single
Variable)
Here's an example of how to plot both a linear and nonlinear equation using MATLAB:
Linear Equation:
A simple linear equation in one variable can be:
f ( x )=2 x +5
Nonlinear Equation:
A simple nonlinear equation could be:
2
g ( x )=x − 3 x +2
% MATLAB code to plot linear and nonlinear equations

% Define the range for x values


x = linspace(-10, 10, 400); % x from -10 to 10 with 400 points

% Define the linear and nonlinear functions


f_linear = 2 * x + 5; % Linear equation: f(x) = 2x + 5
f_nonlinear = x.^2 - 3 * x + 2; % Nonlinear equation: f(x) = x^2 - 3x
+ 2

% Create the plot


figure;

% Plot the linear equation


subplot(1, 2, 1); % Subplot for the linear equation
plot(x, f_linear, 'b-', 'LineWidth', 2); % Blue line for the linear
equation
title('Linear Equation: f(x) = 2x + 5');
xlabel('x');
ylabel('f(x)');
grid on;

% Plot the nonlinear equation


subplot(1, 2, 2); % Subplot for the nonlinear equation
plot(x, f_nonlinear, 'r-', 'LineWidth', 2); % Red line for the
nonlinear equation
title('Nonlinear Equation: f(x) = x^2 - 3x + 2');
xlabel('x');
ylabel('f(x)');
grid on;
% Adjust layout
sgtitle('Linear and Nonlinear Equations');

This code will:


1. Plot both a linear and a nonlinear equation on the same figure, side by side.
2. The linear equation is f ( x )=2 x +5, and the nonlinear equation is g ( x )=x 2 − 3 x +2.

Step 2: 5 Equations with 5 Unknowns


Here are 5 linear equations with 5 unknowns that can be solved using MATLAB:
1. 2 x1 +3 x 2 − x3 + 4 x 4 − x 5=10
2. x 1 − 2 x 2 +3 x3 − 4 x 4 + x 5=5
3. 4 x1 − x 2 +2 x 3 + x 4 −3 x 5=8
4. −3 x 1 +5 x2 −2 x 3+ x 4+ 2 x 5=12
5. x 1+ x2 + x 3 + x 4 + x 5=15

In MATLAB, you can solve this system using the linsolve function or matrix division \.
MATLAB Code to Solve the System of Linear Equations:
% Define the coefficient matrix (A) and the right-hand side vector (B)
A = [2 3 -1 4 -1; % Coefficients of the first equation
1 -2 3 -4 1; % Coefficients of the second equation
4 -1 2 1 -3; % Coefficients of the third equation
-3 5 -2 1 2; % Coefficients of the fourth equation
1 1 1 1 1]; % Coefficients of the fifth equation

B = [10; 5; 8; 12; 15]; % Right-hand side values for each equation

% Solve the system of equations


x = A \ B;

% Display the solution


disp('Solution for the unknowns x1, x2, x3, x4, x5:');
disp(x);

Explanation:
 The matrix A contains the coefficients of the variables in each equation.
 The vector B contains the constants on the right-hand side of each equation.
 The system of equations is solved using the matrix division operator \, which is
equivalent to solving A x=B.
This MATLAB code will output the values for x 1 , x 2 , x 3 , x 4 , x 5 that satisfy the system of linear
equations.
Conclusion:
 The first section of code demonstrates how to plot a linear and a nonlinear equation in
MATLAB.
 The second part gives an example of a system of 5 linear equations with 5 unknowns and
how to solve it using MATLAB's matrix capabilities.
Let me know if you need further explanations or modifications!

You might also like