plot a function having two variables with unequal elements
1 view (last 30 days)
Show older comments
hello
i have a fuction as follows
XT=k*L*pitch;
where L contains 100 elements and pitch contains 5 elements. k also contains 5 elements.
i want to create this function and also do plot(L,XT).
it will be a great help if anyone can help me in this.
4 Comments
Walter Roberson
on 7 Apr 2020
Assuming L is 1 x 100 and k is 1 x 5, then k.' * L would be (1 x 5 transposed) * (1 x 100) = 5 x 1 * 1 x 100 -> 5 x 100.
Now you want to multiply that by 1 x 5 pitch. If you pre-multiply, pitch * (k.' * L) then that would be 1 x 5 * 5 x 100 which would give 1 x 100, but you want 5 x 100 or 100 x 5 output. If you post-multiply then that would be 5 x 100 * 1 x 5 and you can only get that to work with some transposes, but you are going to end up with 100 x 1 or 1 x 100 again.
You will need to show us the mathematical form of the calculation you want.
Answers (1)
the cyclist
on 7 Apr 2020
Merging the code you've provided with Walter's comments:
pitch=[30 40 50 60 70]' ;
L=linspace(1,1000);
delta1 = 1;
W1 = 1;
U1 = 1;
a = 1;
V = 1;
K1 = 1;
Rb = 1;
B1 = 1;
N11=sqrt(delta1).*U1.^2.*sqrt((pi*a)./(pitch.*W1)).*exp(-pitch.*W1/a);
D=a*V^3.*K1.^2;
kpq1=N11./D;
XT1=(2.*kpq1.^2*Rb.*L)./(pitch.*B1);
figure
h = plot(L,XT1,'.-');
set(h,'MarkerSize',16)
A couple things to note.
First, there are five curves there. Presumably they will be separated when you put in your actual constants.
As Walter suggested, I have reoriented pitch so that it is 5x1 instead of 1x5. This is the crucial element you need to understand. If you try to multiply
rand(1,5) .* rand(1,100)
then you are going to get a dimension mismatch error. But if you do
rand(5,1) .* rand(1,100)
then MATLAB is going to use implicit expansion, and effectively make both of those arrays into 5x100 arrays before multiplying them. That is what happens in the calculation of XT1 in the code I posted.
Then, the syntax of the plot command will plot five lines (with 100 points each) when you input the 5x100 array. (I used large dots to illustrate this.)
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!