Why I am unable to run my program?

Hi, I really need help. I cannot run my program. When I run, it shows ' Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 121-by-1'. Please, I really need help.
%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(u);
%figure(1)
%plot(u(:,1),r,'-',u(:,2),r,'-',u(:,3),r,'-',u(:,6),r,'-')
%tittle('velocity within explicit method')
%xlabel('r')
%label('u')
%legend('dt=1','dt=2','dt=3','dt=6')

2 Comments

John D'Errico
John D'Errico on 28 Dec 2021
Edited: John D'Errico on 28 Dec 2021
When you get an error, don't just tell us what you personally think is important. The fact is, you don't know what is important. else you would not be asking this question.
Paste in the COMPLETE error message. EVERYTHING in red. That will tell us which line was the problem. It would allow us to learn that without trying to run your code, stepping through every line to see when it fails, IF it fails.
Anyway, we cannot even try to run your code, since I see the variable t was never defined. So it is not possible to answer your question at the moment. My guess is your problem may be that t is a vector. But how do I know?
If you want help, is there a reason why you would want to make it more difficult (and even impossible) to get help?
There is no error if you assign correct values to variables r and t

Sign in to comment.

 Accepted Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fprintf('Beginning to run %s.m ...\n', mfilename);
%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
% Define t and r
allTimes = linspace(0, tmax, maxk)
r = 1; % Whatever...?????
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
t = allTimes(j);
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(u, '-', 'LineWidth', 2);
grid on;
fontSize = 16;
xlabel('space', 'FontSize', fontSize)
ylabel('u', 'FontSize', fontSize)

8 Comments

Hi, thank you so much for helping me. I have tried to run the program that you gave and it worked ! But, can I ask, what does the different colour in the graph represent? Thank you again
Your u is a matrix. each line is one column of that matrix.
If you want, you can plot one row or column at a time if you put plot() inside the loop.
Hi, I am sorry, can you show me like where to put the plot() inside the loop? Thank youu
Also, if I want to produce two more graphs but with different values so that I can compare the graphs, where should I change? Can you like show it to me. Thank you again
Hi, I want to ask, how to make the length of x-axis until 1. Example, in the x-axis, i just want 0, 0.5, 1. Because my motif is to plot u vs r and r is actually the radius of the artery and I declare r =1 inside the coding. Can you help me?
@Nur Nadhirah Syed Malik I don't understand. Your matrix u is indexed like u(spaceIndex, timeIndex). So how can you plot any row or any column vs. r, because r is a constant?
plot(), when it has a matrix, plots each column (all rows in each column) as a separate color, so in the plot above it's plotted vs. space and each of the curves is a different time point.
What I plotted was u as a function of space. If you want it as a function of time (x axis) with different spaces as curves with unique colors, do this:
plot(u', '-', 'LineWidth', 2); % Plot transpose of u rather than u
grid on;
fontSize = 16;
xlabel('Time', 'FontSize', fontSize)
ylabel('u', 'FontSize', fontSize)
If you still want it plotted vs r, when r is a single value of 1, then explain how that could be done.
What if I want to plot u vs r? How can I do that
You'd have to vary r. Right now it's a constant.
How to do that? Can you help me?

Sign in to comment.

More Answers (1)

Kevin
Kevin on 28 Dec 2021
I have tried running your code. MATLAB complains the variable "t" is not defined in the line inside the double for-loop:
u(1,j+1) = u(1,j) + delta_t*(A0 + A1*cos(omega*t) + Beta1*((S10) + 1/r* (S20)));
To debug this kind of problem, use the MATLAB editor to step through the code. You can set breakpoint.
Kevin

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!