Matrix help in MatLab, Not the simple matrix I have searched, found nothing

In summary, In MatLab, you need to nest the for loops if you want to go along the matrix like you're imagining. If you get the same error using 'clear all' then that's your problem. You also want to put 'clear all' at the very beginning of your code, which will mimic having an empty workspace.
  • #1
frozenguy
192
0
Matrix help in MatLab, Not the simple matrix! I have searched, found nothing!

Please help me with this.

I need to code into MATLAB a function that results in a matrix answer.

It is this:
phi(i,j)=atan((eta(j)-eta(i))/(zeta(j)-zeta(i)))*180/pi]

Here is my code so far:
for i=1:4;

N=4;

w(i)=(360/N)*(i-0.5);

x(i)=-cos(w(i)*pi/180);

y(i)=sin(w(i)*pi/180);

x(5)=x(1);

y(5)=y(1);

zeta(i)=0.5*(x(i)+x(i+1));

eta(i)=0.5*(y(i)+y(i+1));

theta(i)=atan2((y(i+1)-y(i)),(x(i+1)-x(i)))*180/pi;

s(i)=sqrt((x(i+1)-x(i))^2+(y(i+1)-y(i))^2);

if theta(i)<0;

theta(i)=theta(i)+360;

end
j=1:4;

phi(i)=[atan((eta(i:N)-eta(i:N))/(zeta(i:N)-zeta(i:N)))*180/pi];

end

See I need a 4X4 matrix for phi.

I only have four etas and four zetas. both i and j = 4. With some work I can get a 4x4 matrix but it isn't coded right. It gives me the same answers in rows. It should have a diagonal line of zero's from top left to bottom right because that is where i=j

And I need to be able to change N to 250 so it needs to be easy to write for large N's

Thank you for your help!
 
Last edited:
Physics news on Phys.org
  • #2


Something is wrong with MatLab. Both my copy and the one at school. I have been using it only for a day or so and I have found a big bug.

When I restart MatLab, my code that was working just before quitting won't work and will get error.

All I have to do is cut it from the file, run the file, then put it back in and presto. No error.

What a complete waste of time. Anyways, I constructed my matrix using this:

phi(i,j)=atan2((eta(j)-eta(i)),(zeta(j)-zeta(i)))*180/pi;

And right above that I have j=1:4

My matrix looks like this:
0 -45.0000 -90.0000 -135.0000
135.0000 0 -135.0000 -180.0000
90.0000 45.0000 0 135.0000
45.0000 0.0000 -45.0000 0

Which is basically correct BUT! Because of atan2 function, those negatives need 360 deg added. So this is what I did (worked on theta earlier in code). But it won't work this time for phi(i,j):
if phi(i,j)<0;

phi(i,j)=phi(i,j)+360
end
 
  • #3


the "error" your having might have simply been that you had variables in your work space from previous runs and playing in the command line. When you restart, you wipe your work space.

Try putting 'clear all' at the very beginning of your code, which will mimic having an empty workspace (like when you first start matlab). If you get the same error using 'clear all' then that's your problem. It means you're missing something in your code that you took for granted.

Also, you want to nest the for loops if you want to go along the matrix like you're imagining:

for i = i range
for j = j range

stuff
phi(i,j) = blahblah
stuff

end
end
 
  • #4


Pythagorean said:
the "error" your having might have simply been that you had variables in your work space from previous runs and playing in the command line. When you restart, you wipe your work space.

Try putting 'clear all' at the very beginning of your code, which will mimic having an empty workspace (like when you first start matlab). If you get the same error using 'clear all' then that's your problem. It means you're missing something in your code that you took for granted.

Also, you want to nest the for loops if you want to go along the matrix like you're imagining:

for i = i range
for j = j range

stuff
phi(i,j) = blahblah
stuff

end
end
What is nesting the for loops?

And also, why am I getting negative angles for phi when I said
If phi(i,j)<0
phi(i,j)=phi(i,j)+360
end

I can say
else phi(i,j)=phi(i,j)+360 but then it just adds 360 to everything.

I just need to add 360 to phi only if phi is negative.
 
  • #5


Hello,

Thank you for reaching out for help with your MATLAB code. It seems like you are trying to create a matrix of angles using the atan function. Here are a few suggestions to help you achieve your desired output:

1. Make sure your indexing is correct. In your code, you have used i=1:4, which means you are only looping through the first four elements of your vectors. If you want to loop through all elements, you can use for i=1:length(zeta).

2. Use the element-wise division operator "./" instead of the regular division operator "/". This will ensure that each element in your matrix is calculated separately.

3. To create a diagonal matrix of zeros, you can use the function diag. For example, if you want a 4x4 matrix with zeros on the diagonal, you can use diag(zeros(1,4)). This will create a 4x4 matrix with zeros on the diagonal and zeros everywhere else.

4. To make your code more efficient for larger N values, you can use vectorization instead of for loops. This will make your code run faster and also make it easier to change N to different values.

I hope these suggestions help you with your code. If you still have trouble, please feel free to reach out for further assistance. Happy coding!
 

What is the purpose of using matrices in MatLab?

Matrices are commonly used in MatLab to represent and manipulate data. They allow for efficient storage and computation of large datasets, making it easier to perform complex mathematical operations and analyze data.

How do I create a matrix in MatLab?

To create a matrix in MatLab, you can either use the matrix function or directly input the values into a variable. For example, A = [1 2 3; 4 5 6; 7 8 9] creates a 3x3 matrix with the values 1, 2, 3 in the first row, 4, 5, 6 in the second row, and 7, 8, 9 in the third row.

Can I perform operations on matrices in MatLab?

Yes, MatLab has a wide range of built-in functions and operators for manipulating matrices. Some common operations include addition, subtraction, multiplication, and transposition. You can also use functions like det and inv to calculate the determinant and inverse of a matrix, respectively.

How do I access specific elements in a matrix?

In MatLab, you can access specific elements in a matrix by using indexing. For example, to access the element in the second row and third column of a matrix A, you can use A(2,3). Matrices in MatLab are indexed starting from 1, not 0.

Can I use matrices in plotting and visualizations?

Yes, MatLab has functions that allow you to plot and visualize data stored in matrices. For example, the plot function can be used to create 2D line plots, and the surf function can be used to create 3D surface plots based on matrix data.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
937
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top