How to plot array graph based on the equation?

Hi, I need help on how to plot array graph based on the coding that I have program it?
%Parameters to define the governing casson fluid equation and the
%parameters value range
L = 1; % Length of the artery
maxk= 10; % Number of time steps
tmax = 0.1; % Maximum time
delta_t = tmax/maxk; % Time step
n = 10; % Number of space steps
delta_x = L/n; % Radial direction
%Initial conditions of velocity
for i = 1:n+1
u(i,1) = (i*delta_x)^2 + 2;
%disp(u(i,1));
end
% Boundary conditions
for k=1:maxk+1
u(1,k) = 2+4*(k*delta_t);
u(n,k) = 3+4*(k*delta_t);
end
% Implementation of the explicit
for k=1:maxk % Time Loop
for i=2:n % Space Loop
%S10 = (u(2,k)-2*u(1,k)+u(2,k))/((delta_x)^2);
%S20 = (u(2,k)-u(2,k))/(2*delta_x);
%u(1,k+1) = u(1,k)+ delta_t*(S10+S20+2-2*(1*delta_x));
%disp(u(1,k+1))
S1 = (u(i+1,k)-2*u(i,k)+u(i-1,k))/((delta_x)^2);
S2 = (u(i+1,k)-u(i-1,k))/(2*delta_x);
u(i,k+1) = u(i,k)+ delta_t*(S1+S2+2-2*(i*delta_x));
disp(u(i,k+1))
S1n = ((2*delta_x) + u(n,k)-2*u(n,k)+u(n-1,k))/((delta_x)^2);
S2n = ((2*delta_x) + u(n,k)-u(n-1,k))/(2*delta_x);
u(n,k+1) = u(n,k)+ delta_t*(S1n+S2n+2-2*(n*delta_x));
disp(u(n,k+1))
end
end

Réponses (1)

Voss
Voss le 27 Déc 2021
If you want to plot u vs r, with one line for each time, you can do
plot(u);
or, if you want to plot u vs time, with one line for each r, you can do:
plot(u.');
or, if you want to show u vs r and time, you might try:
pcolor(u);

6 commentaires

Hi, I try to use plot (u); but the x-axis is from 0 until 11. How can I make the x-axis from 0 0.5 and 1?
Specify the x-coordinates of the plotted lines:
plot(linspace(0,L,n+1),u);
Hi, I tried to use plot(linspace(0,L,n+1),u);as per you mentioned, but when I run it it shows error like:
Error using /
Matrix dimensions must agree.
Error in cassoneq4 (line 41)
u(1,j+1) = u(1,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S10) + 1/r * (S20)));
%Parameters to define the governing casson fluid equation and the
%parameters value range
L= 1; % Length of the artery
maxk= 10; % Number of time steps
tmax = 0.1; % Maximum time
delta_t = tmax/maxk; % Time step
n = 10; % Number of space steps
delta_r = L/n; % Radial direction
Beta1 = 0.025; % Casson fluid parameter
A0 = 0.2; % Amplitude of systolic pressure gradient
A1 = 0.4; % Amplitude of diastolic pressure gradient
omega = pi/4;
%Initial conditions of velocity
for i = 1:n+1
u(i,1) = 0;
disp(u(i,1));
end
% Boundary conditions
for j=1:maxk+1
u(1,j) = 0;
u(n,j) = 0;
end
% Implementation of the explicit
for j=1:maxk % Time Loop
for i=2:n % Space Loop
S10 = (u(2,j)-2*u(1,j)+u(2,j))/((delta_r)^2);
S20 = (u(2,j)-u(1,j))/(delta_r);
u(1,j+1) = u(1,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S10) + (1/r)* (S20)));
disp(u(1,j+1))
S1 = (u(i+1,j)-2*u(i,j)+u(i-1,j))/((delta_r)^2);
S2 = (u(i+1,j)-u(i,j))/(delta_r);
u(i,j+1) = u(i,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S1) + (1/r)* (S2)));
disp(u(i,j+1))
S1n = (u(n+1,j)-2*u(n,j)+u(n-1,j))/((delta_r)^2);
S2n = (u(n+1,j)-u(n,j))/(delta_r);
u(n,j+1) = u(n,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S1n) + (1/r)* (S2n)));
disp(u(n,j+1))
end
end
%Graphical representation of the velocity at different selected times
plot(linspace(0,L,n+1),u);
grid on
title('Velocity vs Radius')
xlabel('Radius(r)')
ylabel('Velocity (m/s)')
Voss
Voss le 31 Déc 2021
That error is unrelated to the plot call.
Try using "1./r" instead of "1/r" in the line where the error happens.
I have tried it, but other error popping out " Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-3.
Error in cassoneqcode1 (line 29)
u(1,j+1) = u(1,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S10) + (1./r)* (S20))); "
%Parameters to define the governing casson fluid equation and the
%parameters value range
L= 1; % Length of the artery
maxk= 10; % Number of time steps
tmax = 0.1; % Maximum time
delta_t = tmax/maxk; % Time step
n = 10; % Number of space steps
delta_r = L/n; % Radial direction
Beta1 = 0.025; % Casson fluid parameter
A0 = 0.2; % Amplitude of systolic pressure gradient
A1 = 0.4; % Amplitude of diastolic pressure gradient
omega = pi/4;
%Initial conditions of velocity
for i = 1:n+1
u(i,1) = 0;
disp(u(i,1));
end
% Boundary conditions
for j=1:maxk+1
u(1,j) = 0;
u(n,j) = 0;
end
% Implementation of the explicit
for j=1:maxk % Time Loop
for i=2:n % Space Loop
S10 = (u(2,j)-2*u(1,j)+u(2,j))/((delta_r)^2);
S20 = (u(2,j)-u(1,j))/(delta_r);
u(1,j+1) = u(1,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S10) + (1./r)* (S20)));
disp(u(1,j+1))
S1 = (u(i+1,j)-2*u(i,j)+u(i-1,j))/((delta_r)^2);
S2 = (u(i+1,j)-u(i,j))/(delta_r);
u(i,j+1) = u(i,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S1) + (1./r)* (S2)));
disp(u(i,j+1))
S1n = (u(n+1,j)-2*u(n,j)+u(n-1,j))/((delta_r)^2);
S2n = (u(n+1,j)-u(n,j))/(delta_r);
u(n,j+1) = u(n,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S1n) + (1./r)* (S2n)));
disp(u(n,j+1))
end
end
%Graphical representation of the velocity at different selected times
plot(linspace(0,L,n+1),u);
grid on
title('Velocity vs Radius')
xlabel('Radius(r)')
ylabel('Velocity (m/s)')
Voss
Voss le 31 Déc 2021
Check your definition of the variable t. It does not appear in the code you showed. Or maybe t should be related to j (the time loop iterator) and delta_t.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by