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

  • Context: MATLAB 
  • Thread starter Thread starter frozenguy
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary

Discussion Overview

The discussion revolves around coding a function in MATLAB to generate a 4x4 matrix based on specific mathematical calculations involving angles. Participants are addressing issues related to matrix construction, handling negative angles, and workspace management in MATLAB.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks assistance in creating a function that calculates a matrix phi based on the formula involving atan, eta, and zeta values.
  • Another participant reports issues with MATLAB, suggesting that restarting the program leads to errors due to residual variables in the workspace.
  • One participant proposes using the atan2 function instead of atan for calculating phi, which yields a matrix but raises concerns about negative angle values.
  • There is a suggestion to use 'clear all' at the beginning of the code to avoid workspace issues.
  • Participants discuss the need to nest for loops to properly iterate over the matrix indices for phi.
  • Concerns are raised about the logic for adjusting negative angles, with one participant questioning why their conditional statement does not work as intended.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the errors encountered in MATLAB, with some attributing it to workspace issues while others focus on the coding logic. The discussion remains unresolved regarding the best approach to handle negative angles in the matrix.

Contextual Notes

Participants mention the need for the code to be adaptable for larger values of N, indicating potential limitations in the current implementation. There is also uncertainty about the correct handling of negative angles and the structure of the nested loops.

frozenguy
Messages
186
Reaction score
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


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
 


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
 


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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K