MATLAB question - meshgrid and sum.

  • Context: MATLAB 
  • Thread starter Thread starter peripatein
  • Start date Start date
  • Tags Tags
    Matlab Sum
Click For Summary

Discussion Overview

The discussion revolves around evaluating a mathematical expression using MATLAB functions such as meshgrid, sum, and dot operations. Participants explore the implementation of the expression and seek clarification on various coding practices and syntax in MATLAB, with a focus on correctness and efficiency.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant presents an expression to evaluate in MATLAB and shares their initial code attempt, expressing uncertainty about its correctness.
  • Another participant questions the basis for the initial participant's confidence in the code being incorrect, noting that the output appears to match expected values.
  • There is a discussion about the nature of the summation in the original expression, with one participant clarifying that the summation yields multiple values corresponding to different inputs.
  • Participants discuss the implications of summing over rows and columns of the resulting matrix and how it relates to the original expression.
  • One participant seeks confirmation on whether their original code adequately answers the question posed.
  • Another participant confirms that the code does indeed evaluate the expression as intended.
  • Subsequent posts introduce various coding questions regarding MATLAB syntax and functionality, including vector operations, random number generation, plotting, and function definitions.
  • Participants express doubts about the efficiency and clarity of their code, seeking feedback on potential improvements.
  • Responses emphasize the importance of functionality over optimization in initial coding efforts, suggesting that clarity and correctness are paramount.

Areas of Agreement / Disagreement

Participants generally agree on the evaluation of the original expression and the correctness of the provided code, though there are ongoing discussions regarding specific coding practices and syntax. Some questions remain unresolved, particularly regarding the efficiency and clarity of the code segments shared later in the thread.

Contextual Notes

Some participants express uncertainty about the syntax and efficiency of their code, indicating that there may be limitations in their understanding of MATLAB functions and operations. The discussion does not resolve all doubts regarding the best practices for coding in MATLAB.

Who May Find This Useful

Individuals interested in MATLAB programming, particularly those working on mathematical evaluations, coding syntax, and optimization in technical contexts.

peripatein
Messages
868
Reaction score
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
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
 
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?
 
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.
 
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?
 
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.
 
Thank you very much!
 
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;?
 
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" ;)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K