I can't seem to call an integral function inside of a plot that has any complexity. I need to plot outputs of a range of values for the integral.

3 views (last 30 days)
I have an integral function that works correctly when I call it once, directly. If I try to call it from a plot, supplying a range of values, it gets an error that doesn't appear sensible. Yes, I see that I pass real parameters into the integral function from outside the object. But as you can see, there is a real linspace parameter passed into the WD_y function inside the plotWD_y function of the weather object. The object isn't the problem. It's specific to how the integral function works.
What I need to do is plot the results of the integral function from a to n (here, 42 to 42+n).
I have made something work from this example. I still cannot get mine to work, with various permutations, including fplot.
https://www.mathworks.com/matlabcentral/answers/467260-how-to-plot-an-integral
I cannot imagine that this can't be done, but it's definitely not straightforward.
The function name in question is highlighted in bold.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example of a plot that works fine.
% Call to plot function.
Wthr.plotWD_y
% called function in the Wthr object.
function plotWD_y(obj)
y=linspace(0,179,1000)
plot(y,obj.WD_y(y))
set(gca,'YScale','log')
title('Log plot of Weather by year')
xlabel('Year (1980=0)')
ylabel('Weather USD (log scale)')
hold off
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This call to integral function works as expected.
tIntgrlPV = Wthr.P_SPV_L_dkn(1.427,42,221)
disp('P_SumPV_L_dkn(d_MFed,42,221) =')
vpa(tIntgrlPV,25)
%**** Output
P_SumPV_L_dkn(d_MFed,42,221) = 255275036562037.6936237779
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This call to integral function from inside a plot doesn't work
a= 42
b= 279
x=linspace(a,b,100)
plot(x,Wthr.P_SPV_L_dkn(1.427, x, b))
%**** Output
Error using ^
Matrix must be square.
IntgrlPV = int( (obj.WD_y(x)/(d+1)^(x-k)), [k, n])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is function called inside the Wthr object.
function IntgrlPV = P_SPV_L_dkn(obj, d, k, n)
% Integral PV
syms x
IntgrlPV = int( (obj.WD_y(x)/(d+1)^(x-k)), [k, n])
end

Answers (1)

Voss
Voss on 13 May 2022
You're calling int with two different sets of inputs and asking why one works and one doesn't, right?
The line:
plot(x,Wthr.P_SPV_L_dkn(1.427, x, b))
generates that error because you should have .^ instead of ^ in P_SPV_L_dkn, and if you change that you'll get a different error that tells you you should have ./ instead of /
If you change both of those you'll get a new error because you're calling int with a vector as a limit of integration.
As the accepted answer in the question you linked to mentions, int returns a constant; you cannot make it return a vector by giving it a vector of limits.
tIntgrlPV = P_SPV_L_dkn(1.427,42,221);
vpa(tIntgrlPV,25)
P_SumPV_L_dkn(d_MFed,42,221) =
ans = 
1.127833209970976279661255
a= 42;
b= 279;
x=linspace(a,b,100);
P_SPV_L_dkn(1.427, a, b) % scalar a and b --> works ok
ans = 
try
% same arguments to P_SPV_L_dkn as when used in plot
% in the question -> same error, nothing to do with plot
P_SPV_L_dkn(1.427, x, b)
catch e
disp(e.message);
end % error message you saw:
Matrix must be square.
% now, with .^ and ./
% --> can't integrate w.r.t. a vector
P_SPV_L_dkn_new(1.427, x, b)
Error using sym/int
Unable to integrate with respect to '[42, 1465/33, 1544/33, 541/11, 1702/33, 1781/33, 620/11, 1939/33, 2018/33, 699/11, 2176/33, 205/3, 778/11, 2413/33, 2492/33, 857/11, 2650/33, 2729/33, 936/11,
2887/33, 2966/33, 1015/11, 284/3, 3203/33, 1094/11, 3361/33, 3440/33, 1173/11, 3598/33, 3677/33, 1252/11, 3835/33, 3914/33, 121, 4072/33, 4151/33, 1410/11, 4309/33, 4388/33, 1489/11, 4546/33,
4625/33, 1568/11, 4783/33, 442/3, 1647/11, 5020/33, 5099/33, 1726/11, 5257/33, 5336/33, 1805/11, 5494/33, 5573/33, 1884/11, 521/3, 5810/33, 1963/11, 5968/33, 6047/33, 2042/11, 6205/33, 6284/33,
2121/11, 6442/33, 6521/33, 200, 6679/33, 6758/33, 2279/11, 6916/33, 6995/33, 2358/11, 7153/33, 7232/33, 2437/11, 7390/33, 679/3, 2516/11, 7627/33, 7706/33, 2595/11, 7864/33, 7943/33, 2674/11,
8101/33, 8180/33, 2753/11, 758/3, 8417/33, 2832/11, 8575/33, 8654/33, 2911/11, 8812/33, 8891/33, 2990/11, 9049/33, 9128/33, 279, 279]'. The integration variable must be a symbolic variable.

Error in solution>P_SPV_L_dkn_new (line 32)
IntgrlPV = int( (1./(d+1).^(x-k)), [k, n]);
function IntgrlPV = P_SPV_L_dkn(d, k, n)
% Integral PV
syms x
IntgrlPV = int( (1/(d+1)^(x-k)), [k, n]);
end
function IntgrlPV = P_SPV_L_dkn_new(d, k, n)
% Integral PV
syms x
IntgrlPV = int( (1./(d+1).^(x-k)), [k, n]);
end

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!