0
$\begingroup$

I'm new to Mathematica and I got stuck. Also, I'm not a programmer, so I would like to ask the following question.

I want to make 1000 different vectors, of which everyone contains 55 random elements. I know how I can do this by using the command RandomReal[100, 54]. Let’s name this vector "s". After that, I want to make another vector, the z=(1/v.s)*s ("v" is a vector with positive numbers, of the same length as "s"). Finally, I want to multiply vector "z" with a 55x55 matrix, let’s say "A", and to be more precise, I want to multiply as follows: (1/(ρ*A).z), where "ρ" is a positive number between 0 and 1. My question is, how can I create and plot (in the same diagram) 1000 different (1/(ρ*A).z) in Mathematica? I know how to do it by hand, but it’s difficult when it comes to 1000 different vectors.

For instance, if I have 5 vectors I apply as follows:

s1=RandomReal[100, 55]

s2=RandomReal[100, 55]

s3=RandomReal[100, 55]

s4=RandomReal[100, 55]

s5=RandomReal[100, 55]

z1=(1/v.s1)s1

z2=(1/v.s2)s2

z3=(1/v.s3)s3

z4=(1/v.s4)s4

z5=(1/v.s5)s5

Plot[{1/ρ*A.z1,1/ρ*A.z2,1/ρ*A.z3,1/ρ*A.z4,1/ρ*A.z5}, {ρ,0,1}]

How can I apply the above command for 1000 "z" without creating them by hand?

$\endgroup$
6
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jul 29, 2021 at 18:32
  • $\begingroup$ Try using Table with z a 1000 element list. $\endgroup$
    – bbgodfrey
    Jul 29, 2021 at 18:34
  • $\begingroup$ What do you mean by (1/(ρ*A).z)? I would think that (ρ*A).z is a vector from your description; what do you mean by the inverse of it? Can you edit your question to explicitly provide what you're trying to calculate using LaTeX? (This forum uses MathJax, so it should display correctly.) $\endgroup$ Jul 29, 2021 at 20:07
  • $\begingroup$ Also, note that your current code is calculating the components of $\frac{1}{\rho} (A\cdot z)$, and then plotting each component separately ($5 \times 55$ plots.) Is that your intent? $\endgroup$ Jul 29, 2021 at 20:09
  • $\begingroup$ Thank you for your comments! I will try to clarify everything. Unfortunately, I can not use LaTex. However, I can try to explain the expression (1/(ρA).z). 1 is the numerator and (ρA).z is the denominator [(ρ*A).z is a vector]. $\endgroup$
    – Panagiotis
    Jul 29, 2021 at 21:53

2 Answers 2

1
$\begingroup$

You could create the list of values with a table, e.g., after defining the vector v and matrix A,

expressions = Table[s = RandomReal[100, 55]; z = (1/v . s) s; (1/(p*A) . z), {1000}]

then plot these expressions

Plot[expressions,{p,0,1}]

Plotting 1000 expressions might be slow.

$\endgroup$
2
  • $\begingroup$ Thank you for your comment! It is very helpful. However, as I mentioned before, in every calculation, vector "s" must be the same (in the numerator and denominator), i.e. z1=(1/v.s1)s1, or z2=(1/v.s2)s2. If I apply the command as you suggested, then "s" will be different in both the numerator and denominator. It is very important for me, as I'm trying to clarify some relevancy in the problem I'm currently studying. Thank you! $\endgroup$
    – Panagiotis
    Jul 29, 2021 at 22:10
  • $\begingroup$ Try evaluating the table with a small example, e.g., 3 dimensional vectors instead of 55, and 5 cases instead of 1000. Each evaluation of z in the Table uses the same s in numerator and denominator. As requested in other comments, clarifying what you want to plot, e.g., vector or scalar, with a small example will help people help you better. $\endgroup$
    – tad
    Jul 29, 2021 at 22:31
1
$\begingroup$

Use Map (or the "infix" form /@) to apply a given function to each element in a list one at a time. Here's how:

Create a list containing 1000 vectors, each one with 55 components:

svectors = RandomReal[100, {1000, 55}]

Define a v (modify this as you see fit):

v = Range[1, 55]

Apply the given transformation to each element in svectors:

zvectors = (#/(v.#)) & /@ svectors;

The construction (#/(v.#)) & is an "anonymous" function that takes one argument # and does the given transformation on it. So in this context, Mathematica will do the transformation given to each element in svectors and store the result in zvectors.

$\endgroup$
4
  • $\begingroup$ As noted in the comments on your question, I am unclear on exactly what you want to plot; but I will try to remember to come back and edit this answer once that is clarified. $\endgroup$ Jul 29, 2021 at 20:17
  • $\begingroup$ Thank you for your comment! It is very helpful. Αs I mentioned before, the expression (1/(ρA).z) is a fraction. 1 is the numerator and (ρA).z is the denominator (as you can imagine (ρA).z is a vector). Also, in every calculation, vector "s" must be the same (in the numerator and denominator), i.e. z1=(1/v.s1)s1, or z2=(1/v.s2)s2. It is very important for me, as I'm trying to clarify some relevancy in the problem I'm currently studying. As for the plot, I want to clarify if all the curves will be linear regardless of the vectors "z". Thank you! $\endgroup$
    – Panagiotis
    Jul 29, 2021 at 22:10
  • $\begingroup$ @Panagiotis: What do you mean by $1/\vec{v}$ where $\vec{v}$ is a vector? There's not a universal notion of the "inverse" of a vector. $\endgroup$ Jul 29, 2021 at 23:52
  • $\begingroup$ sorry for the late response. It is not an inverse of a vector, but a numerical operation. The truth is that I have no other way to explain what I need to calculate. I think that my very first question is quite analytical about what I want to operate (I believe that the example I gave -about 5 vectors- is very helpful for you to understand). Thank you, again! $\endgroup$
    – Panagiotis
    Aug 2, 2021 at 21:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.