Hello,
I'm quite a matlab dummy and it would be nice, if you could help me with this.
When integrating the following function I get the error "Array indices must be positive integers or logical values.". I think it's about the "f(x)-u_equ(x))" term, which is negative. But as the formula is like this I don't know what to do. Sorry in advance if the question/solution is trivial.
clear all;
n=1;
t1 = 14;
t2 = 25;
len = 20; %length of model
initemp = 10; %initial temperature
steps = 20;
u_equ=zeros(1,steps);
f=zeros(1,steps);
% equilibrium temperature
for x=1:steps
u_equ(x) = t1+((t2-t1)/len).*x;
f(x) = initemp;
end
fun = @(x) (f(x)-u_equ(x)).*sin((n.*pi.*x)/len); %function to integrate
intgr = integral(fun,0,len)

 Risposta accettata

Note that ‘f’ and ‘u_equ’ are vectors, not functions.
So ‘fun’ is now:
fun = @(x) (f-u_equ).*sin((n.*pi.*x)/len); %function to integrate
and ‘intgr’ adds ‘ArrayValued’ to its argument list:
intgr = integral(fun,0,len,'ArrayValued',1)
producing:
intgr =
-57.9324 -64.9352 -71.9380 -78.9409 -85.9437 -92.9465 -99.9493 -106.9521 -113.9549 -120.9578 -127.9606 -134.9634 -141.9662 -148.9690 -155.9718 -162.9747 -169.9775 -176.9803 -183.9831 -190.9859

4 Commenti

Wow. Thank you so much! Such a small change but I would have puzzled over it for days. You made my day!
As always, my pleasure!
Thank you for your compliment!
Plotting ‘f’ is not difficult:
figure
fsurf(f(x,t));
With respect to the coefficients, ‘n’ is a function argument, so getting it from ‘f’ is not a problem. Supply ‘n’ to the function (probably best done in a loop, so ‘n’ is in your workspace and does not have to be an actual function argument), do the integration for each value of ‘n’, and store the results.
Where I am confused is that ‘f’ is a function only of ‘x’.
I apologise for the late reply, however I have been doing my best to understand what you are doing with this, and I simply cannot.
Thank you for your efforts. I had the clue that my question was confusing. I tried something myself and not knowing it better I probably totally missed the function idea and worked with arrays. Here is my solution. As you might be in the topic now and if you like, you can check if I'm totally wrong. Of course I'd rather do a surf plot, but I need to adapt the code.
Again, thank you and here is my code
T1 = 14; %time start
T2 = 25; %time end
L = 20; %length of model (L)
initemp = 10; %initial temperature
xsteps = 10;
k = 2.66/(0.857.*2482); %thermal diffusity
modelstartx = 1.081250000000000; %from head_inc.m
modelhalfwidth = 18.923750000000010;
xstepSize = modelhalfwidth / xsteps;
x_ana = [0 modelstartx:xstepSize:modelhalfwidth 20]; %add. start and end point
t = 11000; %Zeit
nmax = 10; %iterations
f=zeros(1,numel(x_ana));
s=zeros(nmax,numel(x_ana));
u_equ = T1+((T2-T1)/L).*x_ana; % equilibrium temperature
f(:) = initemp; % initial temperature
for n=1:1:nmax
fun = @(x,t) (f-u_equ).*sin((n.*pi.*x)/L); %function to integrate
s(n,:) = (2/L).*integral(fun,0,L,'ArrayValued',1).*sin((n.*pi.*x_ana)/L).*exp(-k.*((n.*pi)/L).*t).^2;
end
sumup = sum(s,1);
u = T1+((T2-T1)/L).*x_ana+sumup;
label_data1{1} = sprintf('%s','analytische Lösung');
plot(x_ana,u,'-','LineWidth',pLineWidth,'Marker','none','MarkerSize',pMarkerSize,'color','black');
hold on

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by