MATLAB Evaluating Functions in MATLAB with x,y Range 1-1000

Click For Summary
To evaluate a function at integer values from 1 to 1000 for both x and y in MATLAB, a nested loop approach is suggested. The code initializes a zero matrix z and uses two for loops to iterate through all combinations of x and y, calculating z as the sum of x and y. An alternative method using meshgrid is also mentioned, which efficiently creates matrices for x and y, allowing for direct computation of z without explicit loops.Additionally, there is a request for help in constructing a specific matrix A with a defined pattern and a corresponding vector v. The matrix A is characterized by a tridiagonal structure, while the vector v is specified to have a repeating pattern that can adapt to various dimensions. The discussion emphasizes the need for solutions that can generalize to any size for both the matrix and vector.
jaejoon89
Messages
187
Reaction score
0
Using MATLAB, how do you evaluate a function at integer values, say 1 to 1000, of x and y? For simplicity, let's say the function is z = x + y.
 
Physics news on Phys.org
Hello

Try a loop within a loop to go through all x values then all y values?

Code:
z = zeros(1000);
for x= 1:1000
    for y = 1:1000
        z(x,y) = x + y;
    end
end
 
Code:
[x y] = meshgrid(1:1000);
z = x + y;
 
Hi
Can anyone help how to write the matrix
A=[ 2 1 0 0 0
0 2 1 0 0
1 0 2 1 0
0 1 0 2 1
0 0 1 0 2]
I need A with the above pattern of any dimension n and also a vector of the form
v=[3 3 4 4 4 4 ...4 3 3]^T where v can be of any dimension. I'll be grateful for your help.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K