Entering Non-Linear System of Equations

6 visualizaciones (últimos 30 días)
Jacob
Jacob el 19 de Mzo. de 2012
Respondida: Rohit el 31 de Jul. de 2024
Hi All. I am fairly new to Matlab and still learning. I am wondering if one may explain to me how I should enter a system of non-linear equations. For instance the equations I have are: 4*x(1)-x(2)+x(3)-x(1)*x(4) = 0; -x(1)+3*x(2)-2*x(3)-x(2)*x(4) = 0; and x(1)^2+x(2)^2+x(3)^2-1 = 0. Basically I want to enter the equations and use the Newton Method to solve using the Jacobian Matrix. This is how I enetered them in Matlab:
clear all
clc
syms x
f1 = 4*x(1)-x(2)+x(3)-x(1)*x(4);
f2 = -x(1)+3*x(2)-2*x(3)-x(2)*x(4);
f3 = x(1)^2+x(2)^2+x(3)^2-1;
F = [f1;f2;f3];
If I tell the program to run at this point, this is the error I happen to get:
??? Error using ==> mupadmex
Error in MuPAD command: Index exceeds matrix dimensions.
Error in ==> sym.sym>sym.subsref at 1366
B = mupadmex('mllib::subsref',A.s,inds{:});
Error in ==> NewtonMethosNonLinear at 5
f1 = 4*x(1)-x(2)+x(3)-x(1)*x(4);
I would appreciate some explanation on this since I don't know how to move next. Thank you.

Respuesta aceptada

Alexander
Alexander el 20 de Mzo. de 2012
I am not sure what exactly your program is doing, but you could try this:
x = sym('x', [4, 1])
instead of syms x. This will generate a 4-by-1 vector of symbolic variables. If you want to substitute values later on, you can do it this way:
substituted_f1 = subs(f1, x, [1, 11, 2, 22])
% substitutes x(1) = 1, x(2) = 11, x(3) = 2, x(4) = 22
  3 comentarios
Alexander
Alexander el 20 de Mzo. de 2012
Which version of MATLAB do you have? This feature has been introduced in R2010b. A quick search on Google suggests that you have an older version. If so, you could try this trick to get the vector:
x = sym(zeros(1, 4));
for k = 1:4
x(k) = sym(sprintf('x%d', k));
end
If you don't need to be flexible on the size of the vector, you can do it even more easy:
x = [sym('x1') sym('x2') sym('x3') sym('x4')]
Jacob
Jacob el 20 de Mzo. de 2012
Thank you. The later trick worked fine. I am using R2010a I think that is the reason I wasn't getting anywhere.

Iniciar sesión para comentar.

Más respuestas (1)

Rohit
Rohit el 31 de Jul. de 2024
The Regular Falsi method, or False Position method, is useful for solving nonlinear equations where you expect the root to lie between two points. Here's a step-by-step explanation and MATLAB code to solve the equation \( x^2 - x + 4 = 0 \) using this method:
### MATLAB Code for Regular Falsi Method
```matlab
% Define the function
f = @(x) x.^2 - x + 4;
% Set the initial interval [a, b]
a = -10; % Lower bound of the interval
b = 10; % Upper bound of the interval
% Set tolerance and maximum number of iterations
tol = 1e-6; % Tolerance for stopping criterion
max_iter = 1000; % Maximum number of iterations
% Check if the function values at the endpoints of the interval have opposite signs
if f(a) * f(b) >= 0
error('Function values at the interval endpoints must have opposite signs.');
end
% Initialize iteration counter
iter = 0;
% Main iteration loop
while abs(b - a) > tol
% Calculate the false position
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
% Evaluate the function at the new point
fc = f(c);
% Check if the root is found exactly
if fc == 0
break; % Solution found
elseif f(a) * fc < 0
b = c; % Update the upper bound
else
a = c; % Update the lower bound
end
% Update iteration counter
iter = iter + 1;
% Check for maximum iterations
if iter > max_iter
warning('Maximum number of iterations reached');
break;
end
end
% Display the result
fprintf('Root found at x = %.6f\n', c);
fprintf('Function value at x = %.6f is f(x) = %.6f\n', c, f(c));
```
### Explanation:
1. **Function Definition**: The function `f` represents the equation \( x^2 - x + 4 \) that you want to solve.
2. **Initial Interval**: The initial interval `[a, b]` should be chosen such that `f(a)` and `f(b)` have opposite signs. This ensures that the root lies within the interval.
3. **Tolerance and Maximum Iterations**: `tol` defines the acceptable error range for the result. `max_iter` limits the number of iterations to prevent infinite loops.
4. **Iteration Loop**: The loop continues until the interval width is less than the tolerance. The false position `c` is calculated each iteration.
5. **Updating the Interval**: Depending on the function value at `c`, the interval `[a, b]` is updated to ensure the root is bracketed.
6. **Result**: Once the loop terminates, the approximate root `c` and the function value at `c` are displayed.
### Important Note:
For the quadratic equation \( x^2 - x + 4 \), it's worth noting that this equation does not have real roots because the discriminant is negative. Hence, the Regular Falsi method will not find a real root but can still demonstrate the iterative process. You might want to choose an interval that ensures `f(a)` and `f(b)` have opposite signs for this method to be applicable in practice.

Categorías

Más información sobre Particle & Nuclear Physics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by