Error Using Residue: Too Many output arguments - matlab

[r, p, k] = residue([1, -14],[1,-1,-2] );
Running the above gives me the correct output.
[a,b,c,d] = residue([2 -16 40 -42], [1 -11 39 -45]);
Running the above gives me Error using residue Too many output arguments.
Can somebody explain why the first function works but the second function gives me an error?
Thanks!

I realize what the problem was. You have to put your output in the form [r, p, k] = residue(a, b).
The answer is in the form:
b = [2 -16 40 -42]; a = [1 -11 39 -45]; [r, p, k] = residue(b,a);

Related

How do I multiply matrix with vector and get result in matrix?

Suppose I have a matrix:
A = [ a, b, c;
d, e, f ];
and a vector:
b = [ x;
y;
z ];
What I want is resultant matrix as:
C = [ a*x, b*y, c*z;
d*x, e*y, f*z ];
How can I do this?
Essentially, I want to multiply matrix (dimension: mxn) with a vector (nx1) and get resultant matrix mxn.
As requested in comments (using octave version 3.8.0):
octave> A = [ 1,2,3;4,5,6]; B=[10;20;30];
octave> A*B
ans =
140
320
octave> A.*B
error: product: nonconformant arguments (op1 is 2x3, op2 is 3x1)
octave> bsxfun(#times, A, B)
error: bsxfun: nonconformant dimensions: 2x3 and 3x1
A = [ 1,2,3;4,5,6];
B = [10;100;1000];
A.*B.'
ans =
10 200 3000
40 500 6000

Apply function by column with several inputs in matlab

Apply a function over a matrix using several columns as arguments to apply function with several imputs.
A = [1 2 3];
B = [4 5 6];
C = [7 8 9];
% This is the function I want use,
bsxfun(#(x,y,z) 1/(sqrt(2*pi*z)) * exp((-(x-y).^2)/(2*z)), A, B, C)
But is not working, only works with two imputs:
bsxfun(#(x,y) x+y, A, B)
The error ouput of bsxfun is:
error: Invalid call to bsxfun. Correct usage is:
-- bsxfun (F, A, B)
which is telling that i can not do this with more than 2 inputs..
The expected calculation of the function is basically:
[1/(sqrt(2*pi*C1))*exp((-(A1-B1).^2)/(2*C1))
1/(sqrt(2*pi*C2))*exp((-(A2-B2).^2)/(2*C2))
1/(sqrt(2*pi*C3))*exp((-(A3-B3).^2)/(2*C3))]
being A1,B1,C1 the first element of A,B,C respectively to N being N the number of elements of the vectors(with same length)
result = [ 0.079 0.08 0.08 ]
You do not need bsxfun here. You just need to define the function handle appropriately.
f = #(x,y,z) 1./(sqrt(2*pi*z)).*exp((-(x-y).^2)./(2*z));
Now your expected result is:
f(A,B,C)

create a polynomial matlab

I have got vector of coefficients v=[v1, v2, v3] (added by user).
I want to create a polynomial in a function. I would like to have a function fun(x), which solution will be my polynomial. After that I want to have a graph of this polynomial.
This is my idea but it doesn't work. Could you have any ideas how to improve it?
function [v] = createPolynomial(x)
r = length(v);
fun=0;
for i=r:1
fun=fun+v(i)*x.^(r-1);
end
You're pretty close! Is this what you want?
function f = createPoly(v,x)
n = length(v);
f = 0;
for ii = 1:n
f = f + v(ii)*x.^(n-ii+1);
end
end
f = createPoly([1 2 3 5],4)
f =
113
%% (1*4^3) + (2*4^2) + (3*4^1) + (5*4^0) = 113
Some errors in your code:
function [v] = createPolynomial(x)
As I understand it, you want both v and x as inputs to your function, and get a value back. Then you must do function value = createPolynomial(v, x), where valuewill be the output variable.
fun=fun+v(i)*x.^(r-1);
I guess this is just a typo, but .^r-1 is a constant value. You probably want the exponent to go from n, n-1, ... 1, 0 In that case you want r-i. And if I don't point it out, someone else will definitely do: Using i as a variable in MATLAB is not good practice if you are sometimes dealing with complex numbers.
And I guess you know this, but I'll say it anyway: You m-file must have the same name as you function.
If you want to input x as a vector, you must initialize f as a vector having the same length as x. That is:
f = zeros(1,length(x));
Now, you can do:
f = createPoly([1 2 3 5],1:5)
f =
11
27
59
113
195
you define coefficients in the following form of variable p
% example :
p =[ 2 1 3] % coefficients
x=0:0.2:5; % values at which it is to be evaluated
y=polyval(p, x);
plot(x,y)
polyval is provided in standard matlab, it evaluates the polynomial. see help polyval

two ways of using symbolic solve in matlab

Why can Matlab solve the following system of equations that way:
eq1 = 'x1+x2+x4=1';
eq2 = 'x3+x4 = 2';
eq3 = '2*x1+3*x2+1*x3+4*x4=3';
eq4 = '3*x1+4*x2+2*x3+6*x4=p';
[p a b c d] = solve(eq1,eq2,eq3,eq4,p,x1,x2,x3,x4);
but not if I use the following code?
A = sym([1 1 0 1; 0 0 1 1; 2 3 1 4; 3 4 2 6]);
x = [x1 x2 x3 x4].';
b = sym([1 2 3 p].');
[p a b c d] = solve(A(1,:)*x==b(1),A(2,:)*x==b(2),A(3,:)*x==b(3),A(4,:)*x==b(4),p,x)
The first thing gives a value for p, and x1 to x4, while the second does not find any solution.
Thanks for an answer!
In R2012b neither of those work. One obtains warnings and, if anything, only parametrized solutions. You should check what you entered.
Why? First, rank(A) returns three, which means that you don't have four independent equations for your four-dimensional system. Second, even if you had four equations, you're trying to solve for five unknowns (p as well). You may need another constraint. Also note that the output of your calls to solve overwrites your symbolic variable p.
If you're trying to solve systems of symbolic equations, don't use solve, look into linsolve. Or, you can solve for p and a subset of x with your non-full rank system:
x = sym('x',[4 1]);
syms p;
A = sym([1 1 0 1; 0 0 1 1; 2 3 1 4; 3 4 2 6]);
b = sym([1 2 3 p].');
[p_ x1 x2 x3] = solve(A*x==b,p,x(1),x(2),x(3)) % results will be function of x4

A Fast and Efficient way to create a matrix from a series of product

Ax, Ay, Az: [N-by-N]
B=AA (a dyadic product)
It means :
B(i,j)= [Ax(i,j);Ay(i,j);Az(i,j)]*[Ax(i,j) Ay(i,j) Az(i,j)]
B(i,j) : a 3x3 matrix.
One way to construct B is:
N=2;
Ax=rand(N); Ay=rand(N); Az=rand(N); %# [N-by-N]
t=1;
F=zeros(3,3,N^2);
for i=1:N
for j=1:N
F(:,:,t)= [Ax(i,j);Ay(i,j);Az(i,j)]*[Ax(i,j) Ay(i,j) Az(i,j)];
t=t+1; %# t is just a counter
end
end
%# then we can write
B = mat2cell(F,3,3,ones(N^2,1));
B = reshape(B,N,N)';
B = cell2mat(B);
Is there a faster way for when N is large.
Edit:
Thanks for your answer. (It's faster)
Let's put:
N=2;
Ax=[1 2;3 4]; Ay=[5 6;7 8]; Az=[9 10;11 12];
B =
1 5 9 4 12 20
5 25 45 12 36 60
9 45 81 20 60 100
9 21 33 16 32 48
21 49 77 32 64 96
33 77 121 48 96 144
Run:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
If I write :P = Ai*Aj; then
B =
7 19 31 15 43 71
23 67 111 31 91 151
39 115 191 47 139 231
10 22 34 22 50 78
34 78 122 46 106 166
58 134 210 70 162 254
That is defferent from above
A(:,:,1) deffer from [Ax(1,1) Ay(1,1) Az(1,1)]
Edit:
N=100;
Me :Elapsed time is 1.614244 seconds.
gnovice :Elapsed time is 0.056575 seconds.
N=200;
Me :Elapsed time is 6.044628 seconds.
gnovice :Elapsed time is 0.182455 seconds.
N=400;
Me :Elapsed time is 23.775540 seconds.
gnovice :Elapsed time is 0.756682 seconds.
Fast!
rwong: B was not the same.
Edit:
After some modification for my application :
by gnovice codes
1st code : 19.303310 seconds
2nd code: 23.128920 seconds
3rd code: 13.363585 seconds
It seems that any function calling like ceil,ind2sub ... make thw loops slow and shoud avoid if possible.
symIndex was interesting! Thank you.
Here's a fairly simple and general implementation that uses a single for loop to perform linear indexing and avoids dealing with 3-dimensional variables or reshaping:
%# General solution:
%# ----------------
B = cell(N);
for index = 1:N^2
A = [Ax(index) Ay(index) Az(index)];
B{index} = A(:)*A;
end
B = cell2mat(B);
EDIT #1:
In response to the additional question of how to reduce the number of calculations performed for a symmetric matrix B, you could use the following modified version of the above code:
%# Symmetric solution #1:
%# ---------------------
B = cell(N);
for index = find(tril(ones(N))).' %'# Loop over the lower triangular part of B
A = [Ax(index) Ay(index) Az(index)];
B{index} = A(:)*A;
symIndex = N*rem(index-1,N)+ceil(index/N); %# Find the linear index for the
%# symmetric element
if symIndex ~= index %# If we're not on the main diagonal
B{symIndex} = B{index}; %# then copy the symmetric element
end
end
B = cell2mat(B);
However, in such a case you may get better performance (or at least simpler looking code) by foregoing the linear indexing and using two for loops with subscripted indexing like so:
%# Symmetric solution #2:
%# ---------------------
B = cell(N);
for c = 1:N %# Loop over the columns
for r = c:N %# Loop over a subset of the rows
A = [Ax(r,c) Ay(r,c) Az(r,c)];
B{r,c} = A(:)*A;
if r ~= c %# If we're not on the main diagonal
B{c,r} = B{r,c}; %# then copy the symmetric element
end
end
end
B = cell2mat(B);
EDIT #2:
The second symmetric solution can be further sped up by moving the diagonal calculation outside of the inner loop (removing the need for a conditional statement) and overwriting A with the result A(:)*A so that we can update the symmetric cell entry B{c,r} using A instead of B{r,c} (i.e. updating one cell with the contents of another appears to carry some extra overhead):
%# Symmetric solution #3:
%# ---------------------
B = cell(N);
for c = 1:N %# Loop over the columns
A = [Ax(c,c) Ay(c,c) Az(c,c)];
B{c,c} = A(:)*A;
for r = c+1:N %# Loop over a subset of the rows
A = [Ax(r,c) Ay(r,c) Az(r,c)];
A = A(:)*A;
B{r,c} = A;
B{c,r} = A;
end
end
B = cell2mat(B);
And here are some timing results for the 3 symmetric solutions using the following sample symmetric matrices Ax, Ay, and Az:
N = 400;
Ax = tril(rand(N)); %# Lower triangular matrix
Ax = Ax+triu(Ax.',1); %'# Add transpose to fill upper triangle
Ay = tril(rand(N)); %# Lower triangular matrix
Ay = Ay+triu(Ay.',1); %'# Add transpose to fill upper triangle
Az = tril(rand(N)); %# Lower triangular matrix
Az = Az+triu(Az.',1); %'# Add transpose to fill upper triangle
%# Timing results:
%# --------------
%# Solution #1 = 0.779415 seconds
%# Solution #2 = 0.704830 seconds
%# Solution #3 = 0.325920 seconds
The big speed-up for solution 3 results primarily from updating the cell contents of B with the local variable A instead of updating one cell with the contents of another. In other words, copying cell contents with operations like B{c,r} = B{r,c}; carries more overhead than I expected.
A = cat(3, Ax, Ay, Az); % [N-by-N-by-3]
F = zeros(3, 3, N^2);
for i = 1:3,
for j = 1:3,
Ai = A(:,:,i);
Aj = A(:,:,j);
P = Ai(:) .* Aj(:);
F(i,j,:) = reshape(P, [1, 1, N^2]);
end
end
%# then we can write
B = mat2cell(F,3,3,ones(N^2,1));
B = reshape(B,N,N)';
B = cell2mat(B);