I'm making a program that is supposed to take two variables in account. x which is defined by x=linspace(10,170,9) and t. For each value of x there should be a function for which I want to use the integral command. But I somehow seem to get errors and cant figure out the problem. Thanks in advance.
Code: ----------------------------------------------
T=@(t,x)(1/sqrt(1-((sin(x./2)).^2).*(sin(t)).^2))
l=0.1; g=9.82; c=0; d=2.*pi;
x=linspace(10,170,9);
f=zeros(size(x));
for i=1:length(x)
f(i)=integral(@(t)T(t,x(i)),c,d);
end
plot(x,f)
Errors: -----------------------------------------------
Error using /
Matrix dimensions must agree.
Error in @(t,x)(1/sqrt(1-((sin(x./2)).^2).*(sin(t)).^2))
Error in @(t)T(t,x(i))
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);

 Réponse acceptée

Geoff Hayes
Geoff Hayes le 17 Nov 2014
Modifié(e) : Geoff Hayes le 17 Nov 2014
Jesper - as the error is originating from the anonymous function, it may be easier to replace it with a "regular" function so that you can debug it. Create a new m file called T.m and place the anonymous function code in this new file as
function [res] = T(t,x)
res = 1/sqrt(1-((sin(x./2)).^2).*(sin(t)).^2);
Put a breakpoint at the initialization of res and re-run your code. When the debugger pauses at the line, look at the inputs - x is a 1x1 double and t is a 1x150 double. So the evaluation of sqrt(1-((sin(x./2)).^2).(sin(t)).^2);* is a 1x150 array, and the division, 1/sqrt(1-((sin(x./2)).^2).(sin(t)).^2);*, is the reason for the error.
You probably just want to do an element-wise division (inversion) of each element in your array, so just replace the above line with
res = 1./sqrt(1-((sin(x./2)).^2).*(sin(t)).^2);
(note the period at 1./). Try executing with the above change and see what happens!

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by