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

1 回表示 (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by