Entering Non-Linear System of Equations
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
Rohit
le 31 Juil 2024
0 votes
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.
Catégories
En savoir plus sur Code Performance dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!