How to Compute f(x) for a=1 and various values of the parameter b on the interval 0<x<3 - matlab

I tried using below codes with no luck
f = 0;
a = 1;
b = [1 2 3.5];
for x = 0:3
f = (a * b * x).^(b-1)*exp(-a*x.^b);
end
disp (f);

Assuming the domain is x and the comparison parameter is b we can loop through the values of b to create three distinct vectors for which the function, f is plotted for. Here the value of b is swapped on each iteration of the for-loop. The resultant end up being f with 3 rows by 4 columns where the columns correspond to the x-values/domain and the rows correspond to the value of parameter, b.
x = (0:3);
a = 1;
B = [1 2 3.5];
for Parameter_Index = 1: length(B)
b = B(Parameter_Index);
f(Parameter_Index,:) = (a.*b.*x).^(b-1).*exp(-a.*x.^b);
end
plot(x,f(1,:),x,f(2,:),x,f(3,:));
xlabel("x"); ylabel("f(x)");
legend("B = " + num2str(B(1)),"B = " + num2str(B(2)),"B = " + num2str(B(3)));
Ran using MATLAB R2019b

Related

Create a matrix with alternate rows from two matrices [MATLAB]

I have the following question:
Write a program that:
Starts by requesting an A matrix by keyboard.
Next, if the number of columns of A is odd, the last column of additional zeros must be added. From this moment matrix, A has an even number (n) of columns.
The program will divide the input matrix into two sub-matrices.
The first sub-matrix (A1) contains the first n / 2 columns of A. The second sub-matrix (A2) has the last n / 2 columns.
Finally, the program must calculate and write on the screen a matrix B that has the rows of A1 in the odd rows and those of A2 in the pairs.
Example code
A = input('Enter a matrix:')
% A = magic(5) % for example
[filA, colA] = size(A);
if rem(colA,2)==1
A = [A, zeros(filA,1)]
colA = colA + 1;
end
A1 = A(:, [1:colA/2])
A2 = A(:, [1+(colA/2):colA])
%B
Here is the solution I propose you:
A = [1 2 3; 1 2 3; 1 2 3; 1 2 3];
[A_r,A_c] = size(A);
if (mod(A_c,2) ~= 0)
A = [A zeros(A_r,1)];
A_c = A_c + 1;
end
off = A_c / 2;
A1 = A(:,1:off);
A2 = A(:,(off+1):A_c);
B = reshape([A1(:) A2(:)].',2*A_r,[])
It makes use of the reshape function for interleaving the rows of the matrices A1 and A2. By omitting the ; at the end of the last line, you let Matlab print the output of the final computation in the console, which is:
B =
1 2
3 0
1 2
3 0
1 2
3 0
1 2
3 0
Using a step by step debugging approach, you can see how every step is being performed.
My solution
clear all, clc;
A = input('Ingrese una matriz:')
[filA, colA] = size(A);
if rem(colA,2)==1
A = [A, zeros(filA,1)]
colA = colA + 1;
end
A1 = A(:, [1:colA/2])
A2 = A(:, [1+(colA/2):colA])
B = A2([1;1]*(1:size(A2,1)),:)
B(1:2:end,:) = A1

Trying to get a MATLAB function to take an array of inputs

I'm trying to call a numerical integration function (namely one that uses the trapazoidal method) to compute a definite integral. However, I want to pass more than one value of 'n' to the following function,
function I = traprule(f, a, b, n)
if ~isa(f, 'function_handle')
error('Your first argument was not a function handle')
end
h = (b-a)./ n;
x = a:h:b;
S = 0;
for j = 2:n
S = S + f(x(j));
end
I = (h/2)*(f(a) + 2*S + f(b)); %computes indefinite integral
end
I'm using; f = #(x) 1/x, a = 1 and b = 2. I'm trying to pass n = 10.^(1:10) too, however, I get the following output for I when I do so,
I =
Columns 1 through 3
0.693771403175428 0.069377140317543 0.006937714031754
Columns 4 through 6
0.000693771403175 0.000069377140318 0.000006937714032
Columns 7 through 9
0.000000693771403 0.000000069377140 0.000000006937714
Column 10
0.000000000693771
Any ideas on how to get the function to take n = 10.^(1:10) so I get an output something like,
I = 0.693771403175428, 0.693153430481824, 0.693147243059937 ... and so on for increasing powers of 10?
In the script where you are calling this from, simply iterate over n
k = 3;
f = #(x)1./x;
a = 1; b = 2;
I = zeros(k,1);
for n = 1:k
I(n) = traprule(f, a, b, 10^n);
end
% output: I = 0.693771403175428
% 0.693153430481824
% 0.693147243059937
Then I will contain all of the outputs. Alternatively you can adapt your function to use the same logic to loop over the elements of n if it is passed
as a vector.
Note, you can improve the efficiency of your traprule code by removing the for loop:
% This loop operates on every element of x individually, and is inefficient
S = 0;
for j = 2:n
S = S + f(x(j));
end
% If you ensure you use element-wise equations like f=#(x)1./x instead of f=#(x)1/x
% Then you can use this alternative:
S = sum(f(x(2:n)));

Vectorize the sum of outer products of coresponding columns of two matrices using Matlab/Octave

Suppose I have two matrices A and B which are made up of column vectors as follows.
A = [a_1,a_2,...,a_N];
B = [b_1,b_2,...,b_N];
Is there any way to vectorize the calculation of the sum of outer products for every column in A with the corresponding column in B. Here is my non-vectorized solution.
S = zeros(size(A,1), size(B,1));
for n=1:N
S = S + A(:,n)*B(:,n)'; % S = S + a_n * b_n'
end
Any help would be greatly appreciated.
you are not clear on what N is, but I assume that N = number of column vectors - which means you are simply doing A * B'
A = rand(3,4);
B = rand(3,4);
N = size(A,2);
S = zeros(size(A,1), size(B,1));
for n=1:N
S = S + A(:,n)*B(:,n)'; % S = S + a_n * b_n'
end
%Check that you are doing A*B'
S == A*B'
>> ans =
1 1 1
1 1 1
1 1 1

How to create vector of binary values based on correspondence between two other vectors in MATLAB [duplicate]

I'm developing a program with MatLab that calculates powers of numbers, adds them together, and then sees if any of the first set of numbers (numbers to powers) equals any of the added numbers to powers. I'm trying to check this for each value in the first array, however, I am getting an output like this:
m =
1
128
2187
16384
78125
279936
823543
2097152
4782969
10000000
for each m value, which is just the result of a simple for loop of the array. So when I go to check if m is in the array, it checks is [1, 128,2187,16384,78125...] in the array, and the answer is no. How can I get it to evaluate each individual entry, like this:
Array n is [1,128,2187,16384]
for m = n
m = 1
Is m in array? No
m = 128
Is m in array? No
m = 2187
Is m in array? Yes
m = 16384
Is m in array? No
end
My code is below:
C = [];
D = [];
E = [];
F = [];
numbers1 = [];
numbers2 = [];
numbers = 10;
powers = 10;
for i = 1:numbers
for j = 3:powers
C = [C;i^j];
end
C = transpose(C);
D = [D;C];
C = [];
end
[~,b] = unique(D(:,1)); % indices to unique values in first column of D
D(b,:); % values at these rows
for i = D
for a = D
E = [E;i+a];
end
E = transpose(E);
F = [F;E];
E = [];
end
[~,b] = unique(F(:,1)); % indices to unique values in first column of F
F(b,:); % values at these rows
for m = D % this is the for loop mentioned above
m
end
Example vectors:
>> m = [1 3 5 9];
n = [5 2 1 4 8];
To check if each element of vector m is in n, use ismember:
>>ismember(m,n)
ans =
1 0 1 0
To get the values, not the indices: use logical indexing on m:
>> m(ismember(m,n))
ans =
1 5
or directly use intersect:
>> intersect(m,n)
ans =
1 5

How to vectorize double loop in Matlab?

y = 0;
for m = 0:variable
for n = 0:m
y = y + f(n,m);
end
end
I vectorized the inner loop this way,
y = 0;
for m = 0:variable
n = 0:m
y = y + f(n,m);
end
This resulted in around 60% speed increase for my code. How do I also vectorize the outer loop?
You are probably looking for the meshgrid function. It is designed to fill in the sort of m by n combinations that it looks like you need. For example:
>> m = 1:4;
>> n = 1:3;
>> [mGridValues, nGridValues] = meshgrid(m,n)
mGridValues =
1 2 3 4
1 2 3 4
1 2 3 4
nGridValues =
1 1 1 1
2 2 2 2
3 3 3 3
This is a little more complicated since your inner loop depends on the value of your outer loop. So you will need to mask out the undesired [n, m] pairs (see below).
Modifying the prototype code that you have provided, you would end up with something like this:
[mValues, nValues] = meshgrid(0:variable, 0:variable); %Start with a full combination of values
mask = mValues >= nValues; %Identify all values where m >= n
mValues = mValues(mask); % And then remove pairs which do not
nValues = nValues(mask); % meet this criteria
y = f(nValues, mValues ); %Perform whatever work you are performing here