How can I plot the following function in MATLAB? - arrays

How can I plot the following function in MATLAB?
x = 0:20:200;
y = 1+((x/8)^(1/3))+((8/x)^(1/3));`
I tried it using plot(x,y), but it doesn't run any output. Any help?

You should change all operators that get x to elements-wise. this is done by adding . before the operator. So * is matrix multiplication, while .* is an element by element multiplication. This is true also for ^ and /. + and - are always element-wise. For .* the two inputs must be the same size and shape, or one of them is a scalar. For .^ and ./ it's better to always use them if you know that you are only dealing with array operations (unless both elements are scalars).
x = 0:20:200;
y = 1+((x./8).^(1/3))+((8./x).^(1/3));
plot(x,y)

Related

Creating a linearly spaced array of a particular size

I am new to MATLAB and currently working on my homework assignment. I am trying to declare the x variable as the following:
Create a linearly spaced array x of size (1 × 200) comprising values ranging from –pi to pi.
I've tried this code:
x=[-pi:200:pi];
but I'm not sure if it's the correct way to do this or not.
You can use linspace as follow:
x = linspace(-pi, pi, 200);
check this out for an example:
https://www.mathworks.com/help/matlab/ref/linspace.html
The other answer shows how to use linspace, this is the correct method.
But you can also use the colon operator and some simple arithmetic to do this:
x = -pi : 2*pi/199 : pi -- This means: go from -π to π in steps of such a size that we get exactly 200 values.
x = (0:199) * (2*pi/199) - pi -- This means: create an array with 200 integer values, then scale them to the right range.
Note that you shouldn't use square brackets [] here. They are for concatenating arrays. The colon operator creates a single array, there is nothing to concatenate it with.

How to select part of complex vector in Matlab

This is probably a trivial question, but I want to select a portion of a complex array in order to plot it in Matlab. My MWE is
n = 100;
t = linspace(-1,1,n);
x = rand(n,1)+1j*rand(n,1);
plot(t(45):t(55),real(x(45):x(55)),'.--')
plot(t(45):t(55),imag(x(45):x(55)),'.--')
I get an error
Error using plot
Vectors must be the same length.
because the real(x(45):x(55)) bit returns an empty matrix: Empty matrix: 1-by-0. What is the easiest way to fix this problem without creating new vectors for the real and imaginary x?
It was just a simple mistake. You were doing t(45):t(55), but t is generated by rand, so t(45) would be, say, 0.1, and t(55), 0.2, so 0.1:0.2 is only 0.1. See the problem?
Then when you did it for x, the range was different and thus the error.
What you want is t(45:55), to specify the vector positions from 45 to 55.
This is what you want:
n = 100;
t = linspace(-1,1,n);
x = rand(n,1)+1j*rand(n,1);
plot(t(45:55),real(x(45:55)),'.--')
plot(t(45:55),imag(x(45:55)),'.--')

Vectorization in Matlab, incomprehensible syntax difference causing failure

I fail to understand why, in the below example, only x1 turns into a 1000 column array while y is a single number.
x = [0:1:999];
y = (7.5*(x))/(18000+(x));
x1 = exp(-((x)*8)/333);
Any clarification would be highly appreciated!
Why is x1 1x1000?
As given in the documentation,
exp(X) returns the exponential eˣ for each element in array X.
Since x is 1x1000, so -(x*8)/333 is 1x1000 and when exp() is applied on it, exponentials of all 1000 elements are computed and hence x1 is also 1x1000. As an example, exp([1 2 3]) is same as [exp(1) exp(2) exp(3)].
Why is y a single number?
As given in the documentation,
If A is a rectangular m-by-n matrix with m~= n, and B is a matrix
with n columns, then x = B/A returns a least-squares solution of the
system of equations x*A = B.
In your case,
A is 18000+x and size(18000+x) is 1x1000 i.e. m=1 and n=1000, and m~=n
and B is 7.5*x which has n=1000 columns.
⇒(7.5*x)/(18000+x) is returning you least-squares solution of equations x*(18000+x) = 7.5*x.
Final Remarks:
x = [0:1:999];
Brackets are unnecessary here and it should better be use like this: x=0:1:999 ;
It seems that you want to do element-wise division for computing x1 for which you should use ./ operator like this:
y=(7.5*x)./(18000+x); %Also removed unnecessary brackets
Also note that addition is always element-wise. .+ is not a valid MATLAB syntax (It works in Octave though). See valid arithmetic array and matrix operators in MATLAB here.
‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍3. x1 also has some unnecessary brackets.
The question has already been answered by other people. I just want to point out a small thing. You do not need to write x = 0:1:999. It is better written as x = 0:999 as the default increment value used by MATLAB or Octave is 1.
Try explicitly specifying that you want to do element-wise operations rather than matrix operations:
y = (7.5.*(x))./(18000+(x));
In general, .* does elementwise multiplication, ./ does element-wise division, etc. So [1 2] .* [3 4] yields [3 8]. Omitting the dots will cause Matlab to use matrix operations whenever it can find a reasonable interpretation of your inputs as matrices.

Array - vector multiplication in R

I need a simple matrix-algebra or Kronicker product type operation to multiply an array and a vector in R to get to a specific result. Let's say I have the array:
ar<-array(c(rep(1,9),rep(10,9),rep(100,9)),dim=c(3,3,3))
And the vector c(1,2,3). Multiplying both by * multiplies each row on each slide of the array by 1,2, and 3 respectively. However, I need an operation to get to array
ar2<-array(c(rep(1,9),rep(20,9),rep(300,9)),dim=c(3,3,3))
instead. That is, is there a simple operation that would allow me to transform ar to ar2 using the vector I specified above? Thanks!
ar * rep(1:3, each=9) should work...
For an arbitrary sized array and an arbitrary set of multipliers, you know the dimensions of your array and the axis along which you want to perform your elementwise multiplication (in this case, the z axis):
each_arg <- prod(dim(ar)[1:2])
multipliers <- sample(1:10, 3)
ar2 <- ar * rep(multipliers, each=each_arg)
You can also look at the tensorA package

Matlab : Using each element of a column vector to go through a series of calculations (iterations?)

theta1 = theta(:,1); //This is a column vector of data extracted from an 18x30 matrix theta1 is 18x1.
Then from here I need to go through each of the individual 18 elements one at a time with the following calculations:
nx =((cos(theta1(1))^2)/(1.5^2) + ((sin(theta1(1))^2)/(1.7^2)))^(-1/2);
Here I have selected the first element using "theta1(1)" but ideally I would like to somehow use a for loop so that this calculation can be done for all 18 values of
"theta1" that I have and then produce another 18x1 matrix of "nx". I have tried using a for loop but I can't get it to work correctly.
Then after this I want to use each of the 18 elements of "nx" in another calculation to get another variable:
d = (2*pi*(nx-1.5)*0.000018)/0.000000555;
So, I am looking for some sort of general for loop or any other suitable method that would allow me to do this type of calculation where I use each element in an array and plug it into a formula and produce another array with the answer of the calcualation.
Thanks in advance.
Try using
nx =((cos(theta1(:)).^2)/(1.5^2) + ((sin(theta1(:)).^2)/(1.7^2))).^(-1/2);
d = (2*pi*(nx(:)-1.5)*0.000018)/0.000000555;
Is this what you are looking for?
theta = rand(18,30);
theta1 = theta(:,1);
nx =(cos(theta1.^2)/(1.5.^2) + (sin(theta1.^2)/(1.7^2)).^(-1/2))
d = (2*pi*(nx-1.5)*0.000018)/0.000000555
If you use a . before the ^ operator the vector will be square them element-wise (the same works with .*,./). Look up arithmetic operators

Resources