How to fix a graph with loop

Sorry, I am new to MATLAB, so bear with me. I am trying to plot a graph which shows me the values of Y_B for T = 600:10:850, where k1, k2 and k3 vary with T. Instead, I am given a blank graph with only 599 to 601 on the x axis. Heres my matlab code
MATLAB code
for i = 1:25
T(i) = 600+10i;
k1(i) = 10^7*exp(-12700/T(i));
k2(i) = 5*10^4*exp(-10800/T(i));
k3(i) = 7*10^7*exp(-15000/T(i));
t = 0.2;
Y_B(i) = (k1(i)*t*(k1(i)+k3(i)))/(((k2(i)*t)+1)*(k1(i)+k3(i))*(1+(t*(k1(i)+k3(i)))));
end
plot(T(i),Y_B(i));

 Risposta accettata

Birdman
Birdman il 31 Gen 2018
You do not need for loop:
T=600:10:850;
t = 0.2;
k1 = 10^7.*exp(-12700./T);
k2 = 5*10^4.*exp(-10800./T);
k3 = 7*10^7.*exp(-15000./T);
Y_B = (k1.*t.*(k1+k3))./(((k2.*t)+1).*(k1+k3).*(1+(t.*(k1+k3))));
plot(T,Y_B);

7 Commenti

sophp
sophp il 31 Gen 2018
I see, thank you!... so for example, if I wanted to plot the variation of Y_B with T, for t = 1:0.5:20 would I need a loop then right? How do I display the plot of T vs Y_B for a fixed t and then move onto the next value of t and plot the next one
Yes, then a for loop will do it for you:
T=600:10:850;
t = 0.2;
k1 = 10^7.*exp(-12700./T);
k2 = 5*10^4.*exp(-10800./T);
k3 = 7*10^7.*exp(-15000./T);
t=1:0.5:20;
hold on;
for i=1:numel(t)
Y_B = (k1.*t(i).*(k1+k3))./(((k2.*t(i))+1).*(k1+k3).*(1+(t(i).*(k1+k3))));
plot(T,Y_B);
end
sophp
sophp il 31 Gen 2018
Modificato: sophp il 31 Gen 2018
Ahh great, thanks Birdman, I am now trying to switch my variables to plot t vs Y_B over a range of T. As my k1k2k3 values depends on the value of T, I assumed this must mean they are within the for loop
if true
% code
T=600:10:850;
for i=1:numel(T)
t(i) = 1:2:20;
k1(i) = 10^7.*exp(-12700./T(i));
k2(i) = 5*10^4.*exp(-10800./T(i));
k3(i) = 7*10^7.*exp(-15000./T(i));
Y_B = (k1(i).*t(i).*(k1(i)+k3(i)))./(((k2(i).*t(i))+1).*(k1(i)+k3(i)).*(1+(t(i).*(k1(i)+k3(i)))));
plot(t,Y_B);
end
However this is brings up an error. How can I fix this?
Whenever you mention in the forum, that an error occurs, post a copy of the complete message. Do not let the readers guess, where and what the problem is.
t(i) = 1:2:20;
There is a vector on the right and a scalar on the left. The assignment cannot work.
Just a note: While 5*10^7 is an expensive power operation and a multiplication, 5e7 is a cheap constant. The runtime might not matter here, but it is a good programming practice to avoid unnecessary expensive operations.
Thanks very much Jan for the advice. I have now corrected this
t(i) = [1:2:20];
The error is that in an assignment A(:) = B, the number of elements in A and B must be the same. Is this due to the fact that numel(T) does not equal numel(t)?
@sophp: Remember that [] is the operator for a vertical or horizontal concatenation. While 1:2:20 is a vector, concatenating it with nothing by [1:2:20] replies a vector again. Therefore both produce exactly the same object.
It is not easy to suggest an improvement, because I cannot imagine why you want to assign a vector to the scalar t(i). Most of all the vector is static and does not depend on the loop, so why assigning it in each iteration? What about:
t = 1:2:20;
T = 600:10:850;
for i=1:numel(T)
k1(i) = 1e7 .* exp(-12700 ./ T(i));
k2(i) = 5e4 .* exp(-10800 ./ T(i));
k3(i) = 7e7 .* exp(-15000 ./ T(i));
Y_B = (k1(i) .* t .* (k1(i)+k3(i))) ./ (((k2(i).*t)+1) .* ...
(k1(i)+k3(i)) .* (1+(t(i) .* (k1(i)+k3(i)))));
plot(t, Y_B);
end
I'm not sure if this is what you want. But it is very similar to Bordman's code, which you have accepted already.
sophp
sophp il 31 Gen 2018
Modificato: sophp il 31 Gen 2018
From this I obtain the attached graph, indicating that T has no effect. I expect there to be different operating curves of t vs Y_B as different values of T are used

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Centro assistenza e File Exchange

Richiesto:

il 31 Gen 2018

Modificato:

il 31 Gen 2018

Community Treasure Hunt

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

Start Hunting!

Translated by