필터 지우기
필터 지우기

Plotting Summation Function in matlab

조회 수: 4 (최근 30일)
Daniel Alejandro Diaz
Daniel Alejandro Diaz 2022년 11월 9일
댓글: 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 CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by