Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I have some trouble on setting of n-linear equations in matlab.I don't know how can I declare in matlab.I need matlab code for setting of n-linear equations..
You can write n-linear equations as one matrix equation to solve it. Here you can find great example:
http://blogs.mathworks.com/pick/2007/09/13/matlab-basics-video-solving-linear-equations/ (video!)
See also these pages:
http://en.wikipedia.org/wiki/System_of_linear_equations
http://en.wikipedia.org/wiki/Matrix_equation
You can solve a linear system in various ways, depending on whether there exists a unique solution or not.
A simple way is by reducing it to reduced-echelon form (rref).
Consider the system:
x + 5y = 4
2x - y = 1
You can write the coefficient matrix A, and the RHS, B as follows: (' is the transpose operator)
>> A = [1 5; 2 -1]
A =
1 5
2 -1
>> B = [4 1]'
B =
4
1
You can write it as an augmented matrix (A|B):
>> horzcat(A,B)
ans =
1 5 4
2 -1 1
And then find the REF(A|B)
>> rref(ans)
ans =
1.0000 0 0.8182
0 1.0000 0.6364
And hence x ~ .8182, y ~ .6364.
The absolutely fastest way to solve linear equations in MATLAB is simply to setup your equation on the form
AX = B
and then solve by
X = A\B
You can issue
help mldivide
to find more information on matrix division and what limitations it has.
A Code for iterative method Guase Seidel
tol is error tolerance
x0 is first guess for solution
function seidel(A,b,x0,tol,itmax)
%Solve the system Ax=b using the Gauss-Seidel iteration method.
clc
% =======================================================
% Programmer : A. Ziaee mehr
%
help seidel
n=length(b);
x=zeros(n,1);
%fprintf('\n')
disp('The augumented matrix is = ')
Augm=[A b]
Y=zeros(n,1);
Y=x0;
for k=1:itmax +1
for ii=1:n
S=0;
for jj=1:ii-1
S=S+A(ii,jj)*x(jj);
end
for jj=ii+1:n
S=S+A(ii,jj)*x0(jj);
end
if (A(ii,ii)==0)
break
end
x(ii)=(-S+b(ii))/A(ii,ii);
end
err=abs(norm(x-x0));
rerr=err/(norm(x)+eps);
x0=x;
Y=[Y x];
if(rerr<tol)
break;
end
end
% Print the results
if (A(ii,ii)==0)
disp('division by zero')
elseif (k==itmax+1)
disp('No convergence')
else
%fprintf('\n')
disp('The solution vector are : ')
fprintf('\n')
disp('iter 0 1 2 3 4 ... ');
fprintf('\n')
for ii=1:n
fprintf('%1.0f= ',ii);
fprintf('%10.6f ',Y(ii,1:k+1));
fprintf('\n')
end
fprintf('\n')
disp(['The method converges after ',num2str(k),' iterations to'])
x
end
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I want to construct a 6X6 symmetric matrix A of rank 3 in MATLAB. any suggestions?
tried different ways, couldn't end up with expected result
You can use diag:
diag([1,1,1,0,0,0])
This will produce a diagonal matrix, where the first three entries are ones, and the rest are zeros. Since it is diagonal it is symmetric. And because only three of the diagonal entries are nonzero, it has rank three.
You can try constructing a 6-by-3 matrix H first and using A = H*H' in turn to achieve your objective
>> H = randn(6,3)
H =
1.4090 0.4889 0.8884
1.4172 1.0347 -1.1471
0.6715 0.7269 -1.0689
-1.2075 -0.3034 -0.8095
0.7172 0.2939 -2.9443
1.6302 -0.7873 1.4384
>> A = H*H'
A =
3.0136 1.4837 0.3520 -2.5689 -1.4614 3.1900
1.4837 4.3948 2.9298 -1.0967 4.6978 -0.1542
0.3520 2.9298 2.1218 -0.1661 3.8423 -1.0150
-2.5689 -1.0967 -0.1661 2.2054 1.4282 -2.8940
-1.4614 4.6978 3.8423 1.4282 9.2696 -3.2971
3.1900 -0.1542 -1.0150 -2.8940 -3.2971 5.3464
To verify the resulting A
>> rank(A)
ans =
3
>> ishermitian(A)
ans =
logical
1
I've been set a question asking me to solve a system of linear equations. In the question it states I should set up a matrix A and column vector b to solve the equation Ax=b, where x is the column vector (w x y z).
A = [1 1 1 1; 0 1 4 -2; 2 0 -2 1; 1 -2 -1 1]
b = [28;7;22;-4]
A1 = inv(A).*b
sum(A1,2)
This is what I've done so far, however I know the answer that MATLAB gives me is incorrect, as the right solutions should be w=10.5, x=9, y=2.5, z=6.
Can someone point me in the right direction/ show me where I'm going wrong? (I'm fairly new to MATLAB so very unsure about it all).
Thanks.
A = [1 1 1 1; 0 1 4 -2; 2 0 -2 1; 1 -2 -1 1];
b = [28;7;22;-4];
A1 = A \ b;
ans = sum(A1,2);
For a reference concerning the \ operator, please read this: https://it.mathworks.com/help/matlab/ref/mldivide.html
The correct code to use your technique would be:
A1 = inv(A) * b;
but as you may notice, the Matlab code analyzer will point out that:
For solving a system of linear equations, the inverse of a
matrix is primarily of theoretical value. Never use the inverse of a
matrix to solve a linear system Ax=b with x=inv(A)*b, because it is
slow and inaccurate.
Replace inv(A)*b with A\b
Replace b*inv(A) with b/A
and that:
INV(A)*b can be slower than A\b
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm learning about signal processing and currently I have to do an speech synthesizer in Matlab. For emulate the resonator system of the mouth I've this transfer function:
R(z) = 1 - z ^(-1)
Can I implement this system with filter function in Matlab? I don't know how to extract the coeficients "a" and "b"...
Note: y = filter(b, a, x), where x is the input signal that we have to filter.
Thank you all!
Consulting the documentation for filter, you represent a transfer function as a rational function of coefficients such that:
The desired transfer function you want, Y(z) / X(z) = R(z) is equal to:
R(z) = 1 - z^{-1}
Here a(1) is implicitly equal to 1. Therefore, b(1) = 1 and b(2) = -1 referring to the above equation. All of the coefficients in the denominator are 0 except for a(1) which is equal to 1.
As such, a = 1; b = [1 -1]; and so filtering your signal is simply:
a = 1; b = [1 -1];
y = filter(b, a, x);
x is the signal of interest you want to filter.
This question already has answers here:
solving nonlinear equations in Octave
(2 answers)
Closed 9 years ago.
How to solve a equation like 3^x + 4^x = 6^x in MATLAB . I want the solution exact to eight decimal digits .
I tried a very simplistic way but there is not enough memory for that . Since I know the solution is between 1 and 2 , I thought of creating an array x = [1:10^-9:2] and then use these arrays to find the value of correct x . I know this is very naive method .
How does one go about solving such equations in MATLAB ?
Use fzero:
>> f = #(x) 3^x + 4^x - 6^x
f =
#(x)3^x+4^x-6^x
>> x0 = [1 2]
x0 =
1 2
>> format long g
>> fzero(f,x0)
ans =
1.293174075673
I have got a matrix in a loop which is
m=
-132.5901 -137.2695 -114.1264 -131.4986 -134.5733 Inf
Inf Inf Inf -135.2631 -121.7322 -119.7471
-132.7978 -123.8068 -135.9385 Inf -134.1602 -117.6997
-130.1930 -134.0093 -137.4125 -128.7641 Inf -116.0473
I want a command which can be used in loop to get the answer as:
-132.5901 -137.2695 -114.1264 -131.4986 -134.5733 -119.7471
-132.7978 -123.8068 -135.9385 -135.2631 -121.7322 -117.6997
-130.1930 -134.0093 -137.4125 -128.7641 -134.1602 -116.0473
I don't want a single vector using isfinite command. The matrix size should be retained automatically using Matlab.
I'm going to assume that in retaining the size of the original matrix, you want us to pad the bottom of the solution matrix with NaN. I'm also going to assume that there might be more than one Inf in any given column. In that case the following loop-based solution works for me:
%# Set up an example matrix
M = [Inf 2 Inf 4; 5 Inf 6 7; Inf Inf 8 Inf];
[T, N] = size(M);
%# Get an index of finite elements
I1 = isfinite(M);
%# Solution 1
Soln1 = NaN(T, N);
for n = 1:N
CurCol = M(:, n);
Soln1(1:sum(I1(:, n)), n) = CurCol(I1(:, n));
end
There is probably a fully-vectorized solution to this problem for the general case (there definitely is - see my update below). However, I'd be surprised if it yields much of a speed-up over the loop-based solution. Single loops are now very fast in Matlab IF you set your problem up so you can operate on the columns of a matrix (as opposed to the rows), since these elements are allocated sequentially in memory.
Now, let us assume that there is only one Inf per column. In this case, the problem is much easier to solve, and can be done with the following one-liner:
%# Solution 2
Soln2 = [reshape(M(isfinite(M)), T-1, N); NaN(1, N)];
Solution 2 will obviously fail for the example matrix I set up, since 6 elements can not be re-arranged into a T-1 by N (ie 2 by 4) matrix.
UPDATE: Okay, so for no good reason, it was annoying me that I couldn't do it without a loop. Thus, here is a loop-less solution:
%# Solution 3
Soln3 = NaN(T, N);
I2 = bsxfun(#plus, cumsum(isfinite(M)), (0:T:T*(N-1)));
I2 = I2(:);
I2(I1 == 0) = [];
Soln3(I2) = M(isfinite(M));
Quick (very) non-rigorous speed test on loop versus non-loop solution:
Elapsed time is 0.203997 seconds.
Elapsed time is 0.251969 seconds.
The non-loop solution will probably improve (relatively speaking) though if M is bigger.
Now I need to get back to work :-)
Building on Colin's answer:
>> M = [Inf 2 Inf 6; 5 Inf 8 4; Inf Inf 7 Inf];
>> [N,I] = sort(M)
N =
5 2 7 4
Inf Inf 8 6
Inf Inf Inf Inf
I =
2 1 3 2
1 2 2 1
3 3 1 3
So that N is already a possible end-product.
The data in N are sorted, so if you don't want that, you'll have to un-sort:
>> for ii = 1:size(M,2)
>> N(~isinf(N(:,ii)),ii) = M(~isinf(M(:,ii)),ii);
>> end
N =
5 2 8 6
Inf Inf 7 4
Inf Inf Inf Inf
And, if you really must get rid of the inf, just issue a
N(isinf(N)) = NaN;
somewhere along the lines (and/or replace isinf with isnan where needed).