Matlab: How do you implement this sum? [closed] - matlab

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 6 years ago.
Improve this question
I need to implement the following:
x_i = e ^ (-1 - sum (y_j * A_ji))
where i = 1..10, j = 1..5 and A is a 5x10 matrix (randomly generated).
I tried using symsum but it gave me an index error. Could someone please help me figure out how to implement this?

With
A = rand(5,10); %# random 5x10 array
y = rand(1,5); %# random 1x5 array
Your sum becomes
x = exp( -1 - y*A);
thanks to linear algebra.

Related

Loop vectorization [closed]

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 2 years ago.
Improve this question
I'm applying the filtering function in the frequency domain. Because I am working with large amounts of data, I want to vectorize the for loop shown below. Any help would be appreciated.
N = 2^nextpow2(length(signal));
Y = fft(signal,N);
df=1/(N*dt);
freq=0:df:N/2*df-df;
ff=freq./fccutlow;
H=zeros(1,length(N));
for i=2:length(ff)
H(i)= sqrt((ff(i).^(2*nOrder)))./sqrt(((1+ff(i).^(2*nOrder))));
Y(i)= Y(i).*H(i);
Y(length(Y)-i+2)=Y(length(Y)-i+2).*H(i);
end
Y1= (real(ifft(Y,N)))';
Y1=Y1(1:length(signal));
filt_signal(1,:)=Y1;
For vector arithmetic on whole matrix you do not need any indexing, only use array operators, .*, ./, .^. But when only a part of array is used as operand, you need to use one of indexing methods. Keep in mind that all input and output ranges of arrays should be of compatible sizes.
Check parentheses again, but it is something like this:
H = sqrt((ff.^(2*nOrder)))./sqrt(((1+ff.^(2*nOrder))));
Y= Y.*H;
Y(end-((1:length(ff))+2))=Y(end-((1:length(ff))+2)).*H;
An example: Note that it does not matter if the matrices themselves do not have same sizes, but the operands must have compatible sizes:
A = 1:4; % 1x4
B = magic(3); % 3x3
C = zeros(3, 1); % 3x1
C([1 3]) = A(3:end).*B(1:2, 2)';
Although in this example A, B and C have different sizes, but A(3:end) and B(1:2, 2)' are both 1 by 2 and the code runs with no problem. Try to run it and evaluate different parts of code to see how indexing works.

How to define a function with a matrix [closed]

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 years ago.
Improve this question
I want to define a two variables function g so that g(x,y) be a 2*2 matrix. To do this, I define g(x,y)=[1,1;x,y] but when I put g(1,1) I don't get any answer. How can I evaluate to g?
The code g(x,y)=[1,1;x,y] itself will not do anything. I assume that your expect result will be g=[1,1,1,1]? Therefore you should do as follow:
g=g_func(1,1);
disp(g)
function g=g_func(x,y)
g=[1,1;x,y];
end
It's not that different from the previous answer, but perhaps an anonymous function would meet your needs:
>> g = #(x,y)[1,1;x,y];
>> g(5,6)
ans =
1 1
5 6
Alternatively, if you want g to accept only one input (i.e. a 2-element vector, instead of two scalars), you could do:
g = #(x)[1,1;x(1),x(2)];
% or
g = #(x)[1,1;x(:).'];

need help about solving differential equation [closed]

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 6 years ago.
Improve this question
I have tried to solve this following program in matlab but failed.
clear all
syms y(x)
y=dsolve(2.5e-3*diff(y, 2) + 0.5*diff(y) +122.5*y == 2570);
y=0 , y=20 ;
I want to find value of y.
Assuming this is not simply a copy-past error, you need to specify the location of the boundary values (or potentially initial conditions). The easiest way to do this is within the call to dsolve itself (since I don't know the locations, I'm going to assume y = 0 at x = 0 and y = 20 at x = 1/50):
syms x y(x)
xa = sym(0);
xb = sym('1/50');
y(x) = dsolve(2.5e-3*diff(y, 2) + 0.5*diff(y) + 122.5*y == 2570,y(xa)==0 , y(xb)==20)

Error in MATLAB's plot function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this function
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
where thDesnorm is:
[-23.6608
0.1919
0.1866]
When i want to plot this function this way:
domain = 0:1:100;
figure;
plot(domain, h45(domain));
I get this error:
Error using *
Inner matrix dimensions must agree.
Error in #(x)1/(1+exp(-thDesnorm'*[1,45,x]'))
This function works when calling it with, for example, h45(1).
I guess than in plotting, the function is receiving all the domain vector as the parameter x, and not one by one the values of this vector.
As you guessed, the parameter x is your full array domain. This obviously causes a dimensional error in the dot product thDesnorm'*[1,45,x]. A quick fix would be using arrayfun to evaluate h45. For instance:
thDesnorm = [-23.6608
0.1919
0.1866];
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
domain = 0:1:100;
figure;
plot(domain, arrayfun(#(x)h45(x),domain)) % See modification in h45 call

Matlab cosine mistake [closed]

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 8 years ago.
Improve this question
I've got some problem while equaling such function as:
function czeb()
k = 1:1:5;
Xk = cos( (pi*(k-0.5))/5);
CN(5,Xk)
end
function c = CN(N,x)
a=N*acos(x); % a is equal correct
c = cos(a); % buc c not, why?
return
end
If I view variable a inside CN function I receive
a=[1.5708, 4.7124, 7.8540, 10.9956, 14.1372]
which is correct bun next step in CN function is to calculate cos(a).
In this step I receive incorrect value of cos(a).
It should be
cos(a) = 1.0e-04 *[-0.0367,0.1102,-0.1837,0.2571,-0.3306]
but it is 1.0e-15 * [-0.8269,-0.1837,0.3062,-0.4286,0.5511] and I don't know why...
There is a very simple explanation, and you function is not wrong.
a=[1.5708, 4.7124, 7.8540, 10.9956, 14.1372]
is equal to pi/2 + k * pi. When you take the cosine of a, you will just get zeros. 1.0e-15 * 0.8269 is essentially zero (floating point arithmetic, and rounding errors).
I think, I understand your. Have you have create expected values manual, like cos(1.5708)? If yes, then you will always receive different results with results made by your computer. So, I think, your expected values of array a are incorrect.
First, enable long number format in the MATLAB/Octave:
format long;
After that, if you display your a array, you will see the similar output:
> a
a =
1.57079632679490 4.71238898038469 7.85398163397448 10.99557428756428 14.13716694115407
As you see at this step, your expected values and computed values by MATLAB/Octave are different. Than, if you do cos(1.57079632679490) the result will like -3.49148336110938e-15 and, as you see, this is near to the result of cos(a(1)): -8.26948102009006e-16. This means, that your stored number is different with that you see in the output.
To instead receive the same results as you expect - round your a to the fourth number after comma:
a = round(a*10000)/10000;
With this the computed results should be near to your expected results.