MATLAB question - meshgrid and sum.

  • MATLAB
  • Thread starter peripatein
  • Start date
  • Tags
    Matlab Sum
In summary: The pressure drop is calculated as:PDROP = ex(rho, omega, length, diameter, Reynolds)where rho - fluid's density, omega - fluid's velocity, length - pipe's length, diameter - pipe's diameter, Reynolds - Reynolds number.
  • #1
peripatein
880
0
Hello,
I am asked to evaluate the following expression using functions meshgrid, sum and dot operations in MATLAB:
y = Ʃ(n=1 to N) xn*[cos(x2 + n2)/xn], where x is the vector of four equally spaced values from 1 to 2, N=10.

Below is my attempt (I am quite positive it's incorrect, though):

n = 1:10;
x = linspace(1,2,4);
[X,N] = meshgrid(x,n);
y1 = (X.^N).*((cos(X.^2 + N.^2))./(X.*N));
y2 = sum(y1);

I'd appreciate your assistance!
 
Physics news on Phys.org
  • #2
Why are you so sure that it is wrong?

It looks to me like y2 has 4 values, equal to y(1), y(4/3), y(5/3), y(2).

Code:
y2 =

   -0.1036    1.1696  -11.0536  -81.6197

To check y(1) quickly,

Code:
y = @(x,n) (x.^n).*((cos(x.^2+n.^2))./(x.*n));
for n=1:10
Y(n) = y(1,n);
end
sum(Y)

ans =

   -0.1036
 
  • #3
Thank you for your reply.
You claim that y2 has four values, yet the summation in the original expression (to be evaluated) must yield but one.
Should I then emend my code thus:
y2 = sum(y1(1:10))?
Will that answer the question correctly?
 
  • #4
You claim that y2 has four values, yet the summation in the original expression (to be evaluated) must yield but one.
the summation in the original expression evaluates a different answer for each value of x. n takes on 10 different values, and x takes on 4 different values. So y1 is a 10x4 array where each column represents n=1:10 for a single value of x.

When you sum the columns of y1, you are summing n=1:10 for y(x), just like your expression asks. This results in four different evaluations of the summation, one for each different value of x.

If you were to sum BOTH the rows and columns, it would be equivalent to a double summation over x and n, which is not indicated in your expression.

Should I then emend my code thus:
y2 = sum(y1(1:10))?
Will that answer the question correctly?
No; y1 is a 10x4 matrix as we said before, so it has a total of 40 values, 10 in each of 4 columns. sum(y1(1:10)) does not accomplish what you want.

You know y(1), y(4/3), y(5/3), y(2) from your code already. Perhaps if you provide more information on exactly what question needs to be answered with this code then we can see if this answer is inadequate.
 
  • #5
The question asks for an explicit evaluation of the original sigma expression, using functions meshgrid and sum.
That being the case, are you claiming my original code satisfies this question both adequately and correctly?
 
  • #6
Yes. You want to evaluate

[tex] y(x) = \sum_{n=1)^{10} \frac{x^n \cos(x^2+n^2)}{nx}[/tex]

for 4 different values of x, and this corresponds to y2 in your code.
 
  • #7
Thank you very much!
 
  • #8
Hi Kreil,
Would you be so kind as to help dispel a few minor doubts I am having regarding the code in the following instances:

1) v = r*cos(alpha)*[1 + (r*sin(alpha))/(l*sin(beta))] and alpha changes from 0 to 2pi.
I hence wrote:

alpha = 0:pi/10:2*pi;
v = r*cos(alpha).*(1 + (r*sin(alpha))./(l*sin(beta)));

Should alpha indeed be treated as a vector whilst defining v (I am referring to the use of the dot operations)?

2) Using function rand, should a matrix of random integers in the interval [55..100] be generated thus:

M = ceil((rand(5,5)+(11/9))*45);?

3) Should six concentric circles whose radii vary between 0.5 and 1.75 (with intervals of 0.25) be plotted thus:

theta = linspace(0, 2*pi, 50);
[X, Y] = meshgrid(0.5:0.25:1.75, theta);
plot(a+cos(Y).*X, b+sin(Y).*X);?

4) May I plot what is called a Mobius Strip, whose equation is
x = s*cos(t/2)*cos(t) + cos(t); y = s*cos(t/2)*sin(t) + sin(t); z = s*sin(t/2), where -0.4<=s<=0.4 and 0<=t<=2pi, thus:

s = -0.4: 0.05: 0.4;
t = 0: pi/50: 2*pi;
[X, Y] = meshgrid(s, t);
surf(X.*cos(Y/2).*cos(Y) + cos(Y), X.*cos(Y./2).*sin(Y) + sin(Y), X.*sin(Y./2));?

5) A swimmer is trying to cross a river 1.1 km wide. The velocity of the swimmer is given as 0.95 km/h in the vertical direction, whilst that of the stream is S = 1.3 km/h in the horizontal direction. I am asked for the swimmer's velocity relative to the ground ("true-velocity" vector T) and the speed. May I implement that thus:
AB = 1.1;
V = [0 0.95];
S = [-1.3 0];
T = V + S;
magnitude = norm(T);?

6) I'd like to write a function which calculates the pressure drop given by: P = (1/2)*rho*omega^2*(L/D)*lambda
where rho - fluid's density, omega - fluid's velocity, L - pipe's length, D - pipe's diameter, lambda - function of the Reynolds number.
* For Reynolds<2320, lambda = 64/Reynolds
* For 2320<Reynolds<10^5, lambda = 0.3164/(Reynolds^0.25)
* For 10^5<Reynolds<10^6, 1/sqrt(lambda) = 2lg(Reynolds*sqrt(lambda)) - 0.8, which is to be solved via iteration following this algorithm: letting tol = 0.01 and lambda0 = 1, the starting value x0 should be x0 = 1/sqrt(lambda0) whereas the next value is x = 2lg(Reynolds/x0) - 0.8.
* For Reynolds>10^6, calculations are halted and the program generates a message about that using the error() function.

May I code it thus:

function PDROP = ex(rho, omega, length, diameter, Reynolds)
if (Reynolds<2320)
lambda = 64/Reynolds;
elseif (Reynolds>2320 && Reynolds<10^5)
lambda = 0.3164/(Reynolds^0.25);
elseif (Reynolds>10^5 && Reynolds<10^6)
tol = 0.01;
x0 = 1;
x = 2*log10(Reynolds) - 0.8;
while (abs(x - x0) > tol)
x0 = x;
x = 2*log10(Reynolds/x0) - 0.8;
end
lambda = x^(-2);
elseif (Reynolds>10^6)
error('Reynolds number must not be larger than 10^6');
end
PDROP = 0.5*rho*(omega^2)*(length/diameter)*lambda;?
 
  • #9
What are your doubts? What have you tried? Have you tried running the code and examining the output?

Most of your questions are resolved by running the code and looking at what it produces.
 
  • #10
Well, I have tried executing all the above segments of code, but I am still uncertain the syntax itself is not cumbersome and sufficiently efficient. I have tried to be very clear about the origin and nature of my doubts, in my previous post. I'd gladly try to be even more precise. Would you be willing to answer some of my questions regarding the above code?
 
  • #11
peripatein said:
Well, I have tried executing all the above segments of code, but I am still uncertain the syntax itself is not cumbersome and sufficiently efficient.
If it produces the correct output and you don't have to wait an age for any of it to run, it's surely fine... I don't see how you could make it less cumbersome. Matlab is a nice language for writing concise vectorised operations.

I guess it's worth mentioning a general programming rule: "code first, optimise later". Your code is understandable; if it does what you need it to do and it does it in reasonable time, there is no need for better 'efficiency'. You will lose way too much of your own time for diminishing efficiency returns (especially as these look like problem answers, so they're only going to be run once).
I have tried to be very clear about the origin and nature of my doubts, in my previous post. I'd gladly try to be even more precise. Would you be willing to answer some of my questions regarding the above code?
To be fair, you just asked "may I" or "should I" for most of them... to which the answer is "if it works, yes you may" ;)
 

1. What is meshgrid in MATLAB?

Meshgrid is a function in MATLAB that creates a rectangular grid of coordinates based on input vectors. It is commonly used for creating 2D and 3D plots and performing calculations over a grid of points.

2. How do I use meshgrid in MATLAB?

To use meshgrid in MATLAB, you need to provide two input vectors representing the x and y coordinates of the grid. The function will then create matrices of these coordinates and return them as outputs.

3. What is the purpose of using meshgrid?

The purpose of using meshgrid is to create a grid of coordinates that can be used for plotting or performing calculations. It allows you to easily visualize and analyze data in 2D and 3D space.

4. Can I use meshgrid for higher dimensions?

Yes, meshgrid can be used for creating grids in higher dimensions such as 4D or 5D. However, it can become more complex and difficult to visualize in higher dimensions.

5. How do I sum values over a meshgrid in MATLAB?

To sum values over a meshgrid in MATLAB, you can use the built-in function "sum" along with the matrices created by meshgrid. This will compute the sum of all the values in the grid and return a single scalar value.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
123
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
986
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top