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

Click For Summary

Discussion Overview

The discussion focuses on evaluating a function in MATLAB over a specified range of integer values for variables x and y, specifically from 1 to 1000. Participants explore methods for implementing this evaluation, including both loops and matrix operations. Additionally, there is a request for assistance in constructing a specific matrix and vector pattern.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant asks how to evaluate the function z = x + y for integer values of x and y from 1 to 1000.
  • Another participant suggests using nested loops to iterate through all x and y values, providing a code snippet that initializes a zero matrix and fills it with the sum of x and y.
  • A different approach is presented using the meshgrid function to create matrices for x and y, allowing for direct computation of z without explicit loops.
  • A separate query is made regarding the construction of a specific matrix A with a defined pattern and a vector v that can vary in dimension.

Areas of Agreement / Disagreement

Participants present multiple methods for evaluating the function, indicating a lack of consensus on the preferred approach. The discussion on constructing the matrix and vector remains unresolved, with no responses yet addressing that specific request.

Contextual Notes

The discussion does not clarify the assumptions regarding the dimensions of the matrix A or the specific requirements for the vector v, leaving these aspects open to interpretation.

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
4K
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
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K