Plotting Summation Function in matlab

16 visualizaciones (últimos 30 días)
Daniel Alejandro Diaz
Daniel Alejandro Diaz el 9 de Nov. de 2022
Comentada: Daniel Alejandro Diaz el 9 de Nov. de 2022
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 2022
Editada: Walter Roberson el 9 de Nov. de 2022
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 comentario
Daniel Alejandro Diaz
Daniel Alejandro Diaz el 9 de Nov. de 2022
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!!

Iniciar sesión para comentar.

Más respuestas (1)

Chandler Hall
Chandler Hall el 9 de Nov. de 2022
Does adding the line:
[n, t] = meshgrid(n, t);
sum_parts = ...
produce the desired output?

Categorías

Más información sobre Geographic Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by