[SOS!!!!!!!] Plotting with if statement.
2 ビュー (過去 30 日間)
古いコメントを表示
I have a problem using the if statement. When I plot the it individually, it was fine.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163321/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163322/image.png)
but when I attempt to combine the plots with if statement, it just doesn't seem right.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163323/image.png)
Please help me, I'm stuck!!!! Here's my script:
function [x1,x2,x3,x4]=BurnFraction_2()
clear();
a = 5;
n = 4;
thetas = [-50 -20 0 20];
thetad = 60;
for i = 1:4;
theta=linspace(thetas(i),thetas(i)+thetad,100);
dum=(theta-thetas(i))/thetad;
temp=-a.*dum.^n;
xb=1-exp(temp);
if i == 1
x1 = xb;
end
if i == 2
x2 = xb;
end
if i == 3
x3 = xb;
else
x4 = xb;
end
end;
plot(theta,x1,'-',theta,x2,'--',theta,x3,'.',theta,x4,'-.','linewidth',5);
set(gca,'Xlim',[-60 80],'Ylim',[0 1],'fontsize', 18,'linewidth',2);
xlabel('Crank Angle (°)','fontsize', 18);
ylabel('Cumulative Burn Fraction','fontsize', 18);
end
Thank you! I really appreciate it!!!
1 件のコメント
Stephen23
2016 年 5 月 3 日
編集済み: Stephen23
2016 年 5 月 3 日
Why do you have clear() at the beginning of your function? This is good example of cargo-cult programming: clear does nothing at all here, but is likely put there out of blind faith and habit.
What variables do you imagine that have at the very start of the function, before any variables have been defined? What does clear do ?
Why do beginners think that they need to put clear at the start of every piece of code that they write?
採用された回答
Stephen23
2016 年 5 月 3 日
編集済み: Stephen23
2016 年 5 月 3 日
You made a mistake on these two lines:
theta = linspace(thetas(k),thetas(k)+thetad,100);
dum = (theta-thetas(k))/thetad;
because although you calculate a range of theta values that depends on the loop iteration, you then subtract thetas(k) from theta, so the values in dum are always going to be the same (they do not change with the loop iteration). The rest of your code then correctly plots all four identical vectors in the same location because that is what you calculated.
Try it yourself, it is easy to print the first few values of dum:
disp(dum(1:9))
inside the loop, and you will see that you are always calculating exactly the same values (which you then plot, in exactly the same position, so they look like one line).
Anyway, here is a simplified version of your code (edited after Guillaume's comment):
function [Y,X] = BurnFraction_2()
a = 5;
n = 4;
thetas = [-50 -20 0 20];
thetad = 60;
N = numel(thetas);
X = cell(1,N);
Y = cell(1,N);
for k = 1:N
theta = linspace(thetas(k),thetas(k)+thetad,100);
dum = (theta-thetas(k))/thetad;
temp = -a.*dum.^n;
X{k} = theta;
Y{k} = 1-exp(temp);
end
plot(X{1},Y{1},'-',X{2},Y{2},'--',X{3},Y{3},'.',X{4},Y{4},'-.','linewidth',5);
set(gca,'Xlim',[-60 80],'Ylim',[0 1],'fontsize', 18,'linewidth',2);
xlabel('Crank Angle (°)','fontsize', 18);
ylabel('Cumulative Burn Fraction','fontsize', 18);
which makes this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174234/image.png)
3 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!