Plotting Summation Function in matlab

18 ビュー (過去 30 日間)
Daniel Alejandro Diaz
Daniel Alejandro Diaz 2022 年 11 月 9 日
Hi There,
I am trying to plot my function to see the expected behavior but I am running into trouble. It says the arrays dont match but I cant figure out why. Could someone please help me with this? I think my approach is right but I could be making it more complicated than it needs to be
%% Graphing mathematical model %%
clear all
t = 0:1000;
plot(t,f(t),'--')
hold off
grid
title('Release')
legend('Theoretical')
xlabel('Time (sec)')
ylabel('Concentration (mg/mL)')
function Cal = f(t)
n = 0:250; % Number of summations
Cao = 22.4;
L = 0.01;
Vp = 1*1*2*L;
Dab = .000000468;
Vl = 40;
%Belows is the concentration profile
sum_parts = ((3.*(2.*n+1).*pi.*sin(((2.*n+1).*pi)./2))./(((2.*n+1).*pi)+sin((2.*n+1).*pi))) .* exp(-(((2.*n+1).^2).*(pi.^2).*Dab.*t)/(4.*(L.^2))) .* (sin(((2.*n+1).*pi))./((2.*n+1).*pi)); %Summation
Cal = ((Cao.*Vp)./Vl).*(1-(2./L).*sum(sum_parts,2)); %Final Function
end
Any help is greatly appreciated!!!
-Daniel

採用された回答

Walter Roberson
Walter Roberson 2022 年 11 月 9 日
編集済み: Walter Roberson 2022 年 11 月 9 日
t = 0:1000;
t is a row vector
n = 0:250; % Number of summations
n is a row vector that is a different size than t.
sum_parts = ((3.*(2.*n+1).*pi.*sin(((2.*n+1).*pi)./2))./(((2.*n+1).*pi)+sin((2.*n+1).*pi))) .* exp(-(((2.*n+1).^2).*(pi.^2).*Dab.*t)/(4.*(L.^2))) .* (sin(((2.*n+1).*pi))./((2.*n+1).*pi)); %Summation
That involves row vector n and row vector t, and uses .* between expressions that are length(n) and expressions that are length(t) . With those being different lengths, those are incompatible.
Cal = ((Cao.*Vp)./Vl).*(1-(2./L).*sum(sum_parts,2)); %Final Function
The sum,2 implies you are expecting sum_parts to be either a row vector or else a 2D array. If it were a row vector then sum,2 would give a scalar. If sum_parts is a 2D array then whether the result is length(n) or length(t) depends on whether the array is t x n or n x t.
You plot f(t) versus t, which tells us that you expect the sum to be a vector of length t, either 1 x length(t) or length(t) x 1. And with you having summed sum_parts over the second dimension, we can predict that you intended sum_parts to be length(t) x length(n) and that you want to sum over the different n values.
To have that happen in the context of your code... make t a column vector instead of a row vector, at least inside your function. Such as if you used
t = t(:);
  1 件のコメント
Daniel Alejandro Diaz
Daniel Alejandro Diaz 2022 年 11 月 9 日
Thank you it worked! I really appreciate you for your detailed response. It really helped me understand the issue and more about matlab.
Thanks again!!

サインインしてコメントする。

その他の回答 (1 件)

Chandler Hall
Chandler Hall 2022 年 11 月 9 日
Does adding the line:
[n, t] = meshgrid(n, t);
sum_parts = ...
produce the desired output?

カテゴリ

Help Center および File ExchangeGeographic Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by