error "Inner matrix dimensiion must Agree"

Please I'm trying to plot graphs and varying parameter Gr and t which is temperature. But i keep getting an error "Inner matrix dimensiion must Agree"
>> Gr = [5:5:25];
>> t = [0.2:0.2:1];
>> H = 1;
>> n = 1;
>> N = 5;
>> Pr = 0.72;
>> S = 0.2;
% This is my code
>> U = -Gr*(t.^2/2 + Pr*(N-n)*(t.^4/24 - t.^2/4) + (Pr*(N-n))^2*(t.^6/720 - t.^4/48 + 5*t.^2)...
+ (Pr*(N-n))^3*(t.^8/40320 - t.^6/1440 + 5*t.^4/576 - 61*t.^2/1440) + (Pr*(N-n))^4*(t.^10/36288000 - t.^8/80640 + 5*t.^6/17280 -61*t.^4/17280 + 1385*t.^2/806400)...
- 1/2 + Pr*(N-n)*5/24 - (Pr*(N-n))^2*61/720 + (Pr*(N-n))^3*277/8064 - (Pr*(N-n)^4*(0.014))...
-(S + H - n)*Gr*(t.^4/24 * Pr*(N-n)*(t.^6/720 - t.^4/48) + (Pr*(N - n))^2*(t.^8/40320 - t.^6/1440 + 5*t.^4/576)...
+ (Pr*(N-n))^3*(t.^10/32688000 - t.^8/80640 + 5*t.^6/17280 + 61*t.^4/17280) + (Pr*(N-n))^4*(t.^12/479001600 - t.^10/7257600 + 5*t.^8/967680 - 61*t.^6/518400 + 1385*t.^4/967680)...
- t.^2/4 + Pr*(N-n)*5*t.^2/48 - (Pr*(N-n))^2*61*t.^2/1440 + (Pr*(N-n))^3*277*t.^2/16128 - (Pr*(N-n))^4*(7*t.^2/1000))...
-(1/24 + Pr*(N-n)*(1/720 -1/48) + (Pr*(N-n))^2*(1/40320 - 1/1440 + 5/576) + (Pr*(N-n))^3*(1/36288000 - 1/80640 + 5/17280 - 61/17280) + (Pr*(N-n))^4*(1/479001600 - 1/7257600 + 5/967680 - 61/518400 + 1385/967680)...)
-1/4 + Pr*(N-n)*5/48 - (Pr*(N-n))^2*61/1440 + (Pr*(N-n))^3*277/16128-(Pr*(N-n))^4*(7/100)))
Error using *
Inner matrix dimensions must agree.
>>
>>

 Accepted Answer

DGM
DGM on 17 Dec 2022
Edited: DGM on 17 Dec 2022
You're trying to do matrix multiplication with arrays of incompatible size. You probably mean to do .* elementwise multiplication.
Also, those exponentiations are probably all wrong as well. For example:
1^2/3
ans = 0.3333
1^(2/3)
ans = 1
EDIT: also, this improperly-continued line means that the trailing ) isn't being used. Since the parentheses are balanced, that means that another parentheses is either added or missing somewhere. It's impossible to know where.
+ A^3*(1/36288000 - 1/80640 + 5/17280 - 61/17280) + A^4*(1/479001600 - 1/7257600 + 5/967680 - 61/518400 + 1385/967680)...)

10 Comments

Please what will I do to make the code work, it’s too lengthy and keep giving error, the image below is the formular I want to write
Try this. I make no guarantees that I haven't missed something. EDIT: I've gone back and checked this against the paper as given. I found a few more mistakes, but the expression as printed is still ambiguous.
Gr = 5:5:25; % vectors are orthogonal, allowing implicit expansion to 2D
t = (0.2:0.2:1).';
H = 1;
n = 1;
N = 5;
Pr = 0.72;
S = 0.2;
% pull out common terms
B = Pr*(N-n);
% pull out term blocks
% note that the terms of TB1 begin with an imbalanced (Pr*(N-n)^k, so it's unclear what that means
% i'm going to assume that means (Pr*(N-n))^k, not Pr*(N-n)^k
TB1 = t.^2/2 ...
+ B*(t.^4/24 - t.^2/4) ...
+ B^2*(t.^6/720 - t.^4/48 + 5*t.^2/48)... % 5*t^2/48
+ B^3*(t.^8/40320 - t.^6/1440 + 5*t.^4/576 - 61*t.^2/1440) ...
+ B^4*(t.^10/36288000 - t.^8/80640 + 5*t.^6/17280 - 61*t.^4/17280 + 1385*t.^2/806400) - 1/2 ...
+ B*5/24 ...
- B^2*61/720 ...
+ B^3*277/8064 ...
- B^4*(0.014);
TB2 = t.^4/24 ... % + not *
+ B.*(t.^6/720 - t.^4/48) ...
+ B^2*(t.^8/40320 - t.^6/1440 + 5*t.^4/576)...
+ B^3*(t.^10/32688000 - t.^8/80640 + 5*t.^6/17280 - 61*t.^4/17280) ...
+ B^4*(t.^12/479001600 - t.^10/7257600 + 5*t.^8/967680 - 61*t.^6/518400 + 1385*t.^4/967680) - t.^2/4 ...
+ B*5*t.^2/48 ...
- B^2*61*t.^2/1440 ...
+ B^3*277*t.^2/16128 ...
- B^4*(7*t.^2/1000);
TB3 = 1/24 ...
+ B*(1/720 - 1/48) ...
+ B^2*(1/40320 - 1/1440 + 5/576) ...
+ B^3*(1/36288000 - 1/80640 + 5/17280 - 61/17280) ...
+ B^4*(1/479001600 - 1/7257600 + 5/967680 - 61/518400 + 1385/967680) - 1/4 ...
+ B*5/48 ...
- B^2*61/1440 ...
+ B^3*277/16128 ...
- B^4*7/100;
U = -Gr.*TB1 - (S + H - n)*Gr.*(TB2 - TB3); % elementwise multiply
% plot everything
figure
contourf(Gr,t,U)
colorbar
figure;
hp = plot(t,U,'-d');
lstr = cell(numel(Gr),1);
for k = 1:numel(Gr)
lstr{k} = sprintf('Gr = %d',Gr(k));
end
legend(hp,lstr,'location','southwest')
Obviously that doesn't look like the given plot, but I don't know what else to say. This is how I marked out the expression as I transcribed it:
Each blue term is a product of the form (Pr*(N-n))^p*(subexpression). I'm assuming that this pattern extends to the terms in TB1. Outside the three major terms marked in yellow, the expression is plainly U = -Gr.*TB1 - (S + H - n)*Gr.*(TB2 - TB3);
If I've missed more transcription errors, I don't know where they'd be. If the printed formula is incorrect, I wouldn't know.
Maduka
Maduka on 19 Dec 2022
Edited: Maduka on 19 Dec 2022

I sincerely appreciate your effort,

But My major concern is replicating the graph. Is it possible to use the table value and replicate the exact graph in Matlab?

Obviously, there are mistakes in the formula for U (e.g. missing or misplaced brackets).
So if you give us the correct U, we can replicate your graph.
I guess you can just plot it if that's the correct data.
Gr = 5:5:25; % vectors are orthogonal, allowing implicit expansion to 2D
t = (0:0.2:1).';
U = [0 0 0 0 0;
-0.1015 -0.203 -0.3045 -0.406 -0.5075;
-0.4167 -0.8334 -1.2501 -1.6667 -2.0834;
-0.9861 -1.9722 -2.9583 -3.9444 -4.9305;
-1.8796 -3.7592 -5.6388 -7.5184 -9.398;
-3.2068 -6.4137 -9.6205 -12.8274 -16.0342];
hp = plot(t,U,'-d');
lstr = cell(numel(Gr),1);
for k = 1:numel(Gr)
lstr{k} = sprintf('Gr = %d',Gr(k));
end
legend(hp,lstr,'location','southwest')
DGM how do I remove those dots representing the points on the graph?
I want to remove it
You can remove the markers in the call to plot()
hp = plot(t,U,'-d') % solid line with diamond markers
hp = plot(t,U,'-') % solid line with no markers
Thanks, this really helped
N = [5:5:10 20:30:80];
t = (0:0.2:1).';
U = [0 0 0 0 0;
-0.203 -0.2045 -0.2094 -0.2248 -0.2411;
-0.8334 -0.8742 -0.9593 -1.2573 -1.6266;
-1.9722 -2.1899 -2.6840 -4.737 -7.9306;
-3.7592 -4.51 -6.3789 -16.1119 -35.5583;
-6.4136 -8.4597 -14.1636 -52.4222 -148.671];
hp = plot(t,U,'-');
lstr = cell(numel(N),1);
title('Effect of Radiation on Velocity')
for k = 1:numel(N)
lstr{k} = sprintf('N = %d',N(k));
end
legend(hp,lstr,'location','southwest')
On this plot, how do I convert those lines to curves, so the sharp points won't be noticed
If you just want to smooth the curve and still have it go through the original points, you can do some sort of spline interpolation.
N = [5:5:10 20:30:80];
torig = (0:0.2:1).'; % original t
U = [0 0 0 0 0;
-0.203 -0.2045 -0.2094 -0.2248 -0.2411;
-0.8334 -0.8742 -0.9593 -1.2573 -1.6266;
-1.9722 -2.1899 -2.6840 -4.737 -7.9306;
-3.7592 -4.51 -6.3789 -16.1119 -35.5583;
-6.4136 -8.4597 -14.1636 -52.4222 -148.671];
% interpolate U using a finer t
t = (0:0.05:1).';
U = interp1(torig,U,t,'spline'); % use spline interpolation to smooth the curve
hp = plot(t,U,'-');
lstr = cell(numel(N),1);
title('Effect of Radiation on Velocity')
for k = 1:numel(N)
lstr{k} = sprintf('N = %d',N(k));
end
legend(hp,lstr,'location','southwest')

Sign in to comment.

More Answers (1)

GR = [5:5:25];
T = [0.2:0.2:1];
H = 1;
n = 1;
N = 5;
Pr = 0.72;
S = 0.2;
for i = 1:numel(GR)
Gr = GR(i);
for j = 1: numel(T)
t = T(j);
U(i,j) = -Gr*(t.^2/2 + Pr*(N-n)*(t.^4/24 - t.^2/4) + (Pr*(N-n))^2*(t.^6/720 - t.^4/48 + 5*t.^2)...
+ (Pr*(N-n))^3*(t.^8/40320 - t.^6/1440 + 5*t.^4/576 - 61*t.^2/1440) + (Pr*(N-n))^4*(t.^10/36288000 - t.^8/80640 + 5*t.^6/17280 -61*t.^4/17280 + 1385*t.^2/806400)...
- 1/2 + Pr*(N-n)*5/24 - (Pr*(N-n))^2*61/720 + (Pr*(N-n))^3*277/8064 - (Pr*(N-n)^4*(0.014))...
-(S + H - n)*Gr*(t.^4/24 * Pr*(N-n)*(t.^6/720 - t.^4/48) + (Pr*(N - n))^2*(t.^8/40320 - t.^6/1440 + 5*t.^4/576)...
+ (Pr*(N-n))^3*(t.^10/32688000 - t.^8/80640 + 5*t.^6/17280 + 61*t.^4/17280) + (Pr*(N-n))^4*(t.^12/479001600 - t.^10/7257600 + 5*t.^8/967680 - 61*t.^6/518400 + 1385*t.^4/967680)...
- t.^2/4 + Pr*(N-n)*5*t.^2/48 - (Pr*(N-n))^2*61*t.^2/1440 + (Pr*(N-n))^3*277*t.^2/16128 - (Pr*(N-n))^4*(7*t.^2/1000))...
-(1/24 + Pr*(N-n)*(1/720 -1/48) + (Pr*(N-n))^2*(1/40320 - 1/1440 + 5/576) + (Pr*(N-n))^3*(1/36288000 - 1/80640 + 5/17280 - 61/17280) + (Pr*(N-n))^4*(1/479001600 - 1/7257600 + 5/967680 - 61/518400 + 1385/967680)...)
-1/4 + Pr*(N-n)*5/48 - (Pr*(N-n))^2*61/1440 + (Pr*(N-n))^3*277/16128-(Pr*(N-n))^4*(7/100)));
end
end
contourf(GR,T,U)
colorbar

3 Comments

- (Pr*(N-n))^4*(0.014)
instead of
- (Pr*(N-n)^4*(0.014))
The printed formula is full of imbalanced parentheses, so that's just the way I read it.
EDIT: also, the way U is addressed is transposed with respect to how it's plotted. It's easy to miss since the vectors have the same length.
Please can you help me structure it better?
@DGM please can you help structure the code, this is the nature of graph I’m expecting

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2014a

Asked:

on 17 Dec 2022

Commented:

DGM
on 23 Dec 2022

Community Treasure Hunt

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

Start Hunting!