Create square marix of values for a 2 variable function.

Hello,
I have a two variable function for a given domain i.e. f(x,y) for 0<x<100, 0<y<100 and x and y are discretized I want to form a square matrix where in the columns x remains constant and y varies or vices-versa. I have used the function command to create my function but I am struggling in the next part.
x= 0:0.01:100; y= 0:0.01:100;
function f = f(x,y)
f = cos(x)+sin(y);
end

 Accepted Answer

x= 0:0.01:100; y=(0:0.01:100)';
f=cos(x)+sin(y)
f = 10001×10001
1.0000 1.0000 0.9998 0.9996 0.9992 0.9988 0.9982 0.9976 0.9968 0.9960 0.9950 0.9940 0.9928 0.9916 0.9902 0.9888 0.9872 0.9856 0.9838 0.9820 0.9801 0.9780 0.9759 0.9737 0.9713 0.9689 0.9664 0.9638 0.9611 0.9582 1.0100 1.0099 1.0098 1.0095 1.0092 1.0088 1.0082 1.0076 1.0068 1.0060 1.0050 1.0040 1.0028 1.0016 1.0002 0.9988 0.9972 0.9956 0.9938 0.9920 0.9901 0.9880 0.9859 0.9837 0.9813 0.9789 0.9764 0.9738 0.9711 0.9682 1.0200 1.0199 1.0198 1.0195 1.0192 1.0187 1.0182 1.0175 1.0168 1.0160 1.0150 1.0140 1.0128 1.0116 1.0102 1.0088 1.0072 1.0056 1.0038 1.0020 1.0001 0.9980 0.9959 0.9937 0.9913 0.9889 0.9864 0.9838 0.9811 0.9782 1.0300 1.0299 1.0298 1.0295 1.0292 1.0287 1.0282 1.0275 1.0268 1.0259 1.0250 1.0240 1.0228 1.0216 1.0202 1.0188 1.0172 1.0156 1.0138 1.0120 1.0101 1.0080 1.0059 1.0037 1.0013 0.9989 0.9964 0.9938 0.9911 0.9882 1.0400 1.0399 1.0398 1.0395 1.0392 1.0387 1.0382 1.0375 1.0368 1.0359 1.0350 1.0339 1.0328 1.0316 1.0302 1.0288 1.0272 1.0256 1.0238 1.0220 1.0201 1.0180 1.0159 1.0137 1.0113 1.0089 1.0064 1.0038 1.0010 0.9982 1.0500 1.0499 1.0498 1.0495 1.0492 1.0487 1.0482 1.0475 1.0468 1.0459 1.0450 1.0439 1.0428 1.0415 1.0402 1.0388 1.0372 1.0356 1.0338 1.0320 1.0300 1.0280 1.0259 1.0236 1.0213 1.0189 1.0164 1.0138 1.0110 1.0082 1.0600 1.0599 1.0598 1.0595 1.0592 1.0587 1.0582 1.0575 1.0568 1.0559 1.0550 1.0539 1.0528 1.0515 1.0502 1.0487 1.0472 1.0455 1.0438 1.0420 1.0400 1.0380 1.0359 1.0336 1.0313 1.0289 1.0264 1.0237 1.0210 1.0182 1.0699 1.0699 1.0697 1.0695 1.0691 1.0687 1.0681 1.0675 1.0667 1.0659 1.0649 1.0639 1.0628 1.0615 1.0602 1.0587 1.0572 1.0555 1.0538 1.0519 1.0500 1.0480 1.0458 1.0436 1.0413 1.0389 1.0363 1.0337 1.0310 1.0282 1.0799 1.0799 1.0797 1.0795 1.0791 1.0787 1.0781 1.0775 1.0767 1.0759 1.0749 1.0739 1.0727 1.0715 1.0701 1.0687 1.0671 1.0655 1.0638 1.0619 1.0600 1.0579 1.0558 1.0536 1.0513 1.0488 1.0463 1.0437 1.0410 1.0382 1.0899 1.0898 1.0897 1.0894 1.0891 1.0886 1.0881 1.0874 1.0867 1.0858 1.0849 1.0838 1.0827 1.0814 1.0801 1.0786 1.0771 1.0755 1.0737 1.0719 1.0699 1.0679 1.0658 1.0635 1.0612 1.0588 1.0563 1.0536 1.0509 1.0481

3 Comments

Great! Thank you very much!
Could please explain what the tilde means in terms of the code? Sorry I'm new to matlab.
There is no tilde in the code I posted for you, I don't think...
Apostrophe means "transposed". So you "add" an (1xn) and an (nx1) vector in the above code. In MATLAB, by implicit expansion, this gives an (nxn) matrix.
Example:
x = [1 2];
y = [7;4];
x + y
ans = 2×2
8 9 5 6
[1+7 2+7;
1+4 2+4]
ans = 2×2
8 9 5 6

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 3 Jul 2023

Commented:

on 3 Jul 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!