Matrix with angles that change with each iteration. For loop?

조회 수: 4 (최근 30일)
Carlos So
Carlos So 2015년 6월 15일
댓글: Walter Roberson 2015년 6월 15일
I trying to create a plot using a matrix that changes angles from 0 to 180. I have two issues that I can't figure out. 1 How can I make it so my T value changes every time with each angle, and 2.How do I extract specific values from the resultant matrix sigma? I only want all the values in the first row first column and the first row second column in separate values so I can plot them? Maybe I'm approaching this while thing wrong, below is the code I came up with. Any help is highly appreciated.
c
lc; close all; clear all;
sig = [45 30; 30 -60]
for i = 0:180
T = [cosd(i) sind(i);-sind(i) cosd(i)]
sigma=T*sig*(T)'
primesig = sigma(1,1)
primetau = sigma(1,2)
end
plot(primesig,primetau)
xlabel('sigma');
ylabel('tau');
title('Stress VS Strain');

답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 15일
Your code already uses a different T for each iteration and already extracts sigma values as needed. Your code has a different problem: it overwrites the prime* variables on each iteration. Instead of your code
primesig = sigma(1,1)
primetau = sigma(1,2)
you should have
primesig(i) = sigma(1,1)
primetau(i) = sigma(1,2)
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 6월 15일
primesig(i+1) = sigma(1,1)
primetau(i+1) = sigma(1,2)
Walter Roberson
Walter Roberson 2015년 6월 15일
More generally when you have a list of values that you want to go through, then a useful technique is to assign the list to a variable and step through the variable:
ivals = 0:180; %put values in variable
for K = 1 : length(ivals) %iterate over indices of variable
i = ivals(K); %retrieve the value for this iteration
T = [cosd(i) sind(i);-sind(i) cosd(i)];
sigma=T*sig*(T)';
primesig(K) = sigma(1,1); %store relative to loop count
primetau(K) = sigma(1,2); %store relative to loop count
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by