MATLAB Answers

empty figure for a given code

1 view (last 30 days)
Afnan Mussa
Afnan Mussa on 3 Apr 2021
Edited: DGM on 3 Apr 2021
I am getting an empty figure for the code below, not sure if the range for Theta is correctly written, i tried with Theta= [0, 120] and Theta= [0:10: 120] but still getting empty figure.
I need to get 5 decreasing curves, with a in y axis and Theta in x axis.
clc
global K A ;
K = 482; %g/ L
A = 1.01; % (X0/Y S0)+1
Theta= 0: 120;
a0= 1;
a1 = ((a0*K-a0+K+Theta)- sqrt((Theta.^2)-2*a0*Theta-2*a0*K*Theta-2*K*Theta+(a0^2)+(a0^2)*(K^2)-2*a0*(K^2)+(K^2)-2*(a0^2)*K+2*a0*K))/(2*(K-A+ Theta));
a2 = ((a1*K-a1+K+Theta)- sqrt((Theta.^2)-2*a1*Theta-2*a1*K*Theta-2*K*Theta+(a1^2)+(a1^2)*(K^2)-2*a1*(K^2)+(K^2)-2*(a1^2)*K+2*a1*K))/(2*(K-A+ Theta));
a3 = ((a2*K-a2+K+Theta)- sqrt((Theta.^2)-2*a2*Theta-2*a2*K*Theta-2*K*Theta+(a2^2)+(a2^2)*(K^2)-2*a2*(K^2)+(K^2)-2*(a2^2)*K+2*a2*K))/(2*(K-A+ Theta));
a4 = ((a3*K-a3+K+Theta)- sqrt((Theta.^2)-2*a3*Theta-2*a3*K*Theta-2*K*Theta+(a3^2)+(a3^2)*(K^2)-2*a3*(K^2)+(K^2)-2*(a3^2)*K+2*a3*K))/(2*(K-A+ Theta));
a5 = ((a4*K-a4+K+Theta)- sqrt((Theta.^2)-2*a4*Theta-2*a4*K*Theta-2*K*Theta+(a4^2)+(a4^2)*(K^2)-2*a4*(K^2)+(K^2)-2*(a4^2)*K+2*a4*K))/(2*(K-A+ Theta));
figure
plot(Theta,a0,Theta,a1,Theta,a2,Theta,a3,Theta,a4,Theta,a5);
xlabel('dimensionless residence time')
ylabel('Substrate Fraction')
axis ([0 120 0 1]);
legend('N1','N2','N3','N4','N5')
fh=figure (1);
title ('Substrate conversion with residence time')
disp([a0(1,end),a1(1,end),a2(1,end),a3(1,end),a4(1,end),a5(1,end)])

Answers (2)

darova
darova on 3 Apr 2021
You forgot the dot (element-wise operator)
You can simplify your code using for loop
a0 = 1;
for i = 1:5
a0 = ((a0*K-a0+K+Theta)- % ...
line(Theta,a0)
end

DGM
DGM on 3 Apr 2021
Edited: DGM on 3 Apr 2021
I'm not sure what your formulae should be for your tasks, but keep in mind the geometry of your variables. While a0 is a scalar as defined, if you're expecting to have any curves to plot, these expressions should be producing vectors. Since we're operating on vectors, we need to pay attention to what we mean when we use multiplication, exponentiation and division operators. If we just want to operate on the elements of the vectors, then use elementwise operators, otherwise you're doing array multiplication and the size will change. In this case, a1, a2, etc were all scalars, so there wasn't anything noticeable to plot.
clc; clf
global K A ;
K = 482; %g/ L
A = 1.01; % (X0/Y S0)+1
Theta= 0: 120;
% a0 etc were scalar, but should be vectors. use elementwise ops where appropriate
a0 = ones(size(Theta));
a1 = ((a0*K-a0+K+Theta) - sqrt((Theta.^2) - 2*a0.*Theta - 2*a0*K.*Theta - 2*K*Theta ...
+ (a0.^2)*(1+K^2) - 2*a0*(K^2) + (K^2) - 2*(a0.^2)*K + 2*a0*K))./(2*(K-A+Theta));
a2 = ((a1*K-a1+K+Theta) - sqrt((Theta.^2) - 2*a1.*Theta - 2*a1*K.*Theta - 2*K*Theta ...
+ (a1.^2)*(1+K^2) - 2*a1*(K^2) + (K^2) - 2*(a1.^2)*K + 2*a1*K))./(2*(K-A+Theta));
a3 = ((a2*K-a2+K+Theta) - sqrt((Theta.^2) - 2*a2.*Theta - 2*a2*K.*Theta - 2*K*Theta ...
+ (a2.^2)*(1+K^2) - 2*a2*(K^2) + (K^2) - 2*(a2.^2)*K + 2*a2*K))./(2*(K-A+Theta));
a4 = ((a3*K-a3+K+Theta) - sqrt((Theta.^2) - 2*a3.*Theta - 2*a3*K.*Theta - 2*K*Theta ...
+ (a3.^2)*(1+K^2) - 2*a3*(K^2) + (K^2) - 2*(a3.^2)*K + 2*a3*K))./(2*(K-A+Theta));
a5 = ((a4*K-a4+K+Theta) - sqrt((Theta.^2) - 2*a4.*Theta - 2*a4*K.*Theta - 2*K*Theta ...
+ (a4.^2)*(1+K^2) - 2*a4*(K^2) + (K^2) - 2*(a4.^2)*K + 2*a4*K))./(2*(K-A+Theta));
figure
% plot() is going to only plot the real component. is that what you want?
% you can use real() or imag() to get the components, or abs() to get the magnitude
plot(Theta,real(a0),Theta,real(a1),Theta,real(a2),Theta,real(a3),Theta,real(a4),Theta,real(a5));
%plot(Theta,imag(a0),Theta,imag(a1),Theta,imag(a2),Theta,imag(a3),Theta,imag(a4),Theta,imag(a5));
% you may need to adjust your limits if you use imag() instead
axis ([0 120 0 1]);
xlabel('dimensionless residence time')
ylabel('Substrate Fraction')
legend('N1','N2','N3','N4','N5','location','southwest')
title ('Substrate conversion with residence time')
disp([a0(1,end),a1(1,end),a2(1,end),a3(1,end),a4(1,end),a5(1,end)])
This produces curves, but a1, etc are complex-valued vectors. You'll have to determine if that's expected and how you want to handle it. What component is of importance? By default, plot() only draws the real component.

Community Treasure Hunt

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

Start Hunting!