Quad error, simple function,

I am a beginner, so please bear with me
cpn is referenced as such as this is part of a much larger code
clc
clear all
close all
cpn=[29105 8614.9 1701.6 103.47 909.79];
fun=@ (ll) cpn(1)+cpn(2)*((cpn(3)./ll)/sinh(cpn(3)./ll))^2+cpn(4)*((cpn(5)./ll)/cosh(cpn(5)./ll))^2
fun(100)
fun(200)
Q=quad(fun,18,200)
I am getting this error:
Error using quad (line 71) The integrand function must return an output vector of the same length as the input vector.
Error in Inttry (line 11) Q=quad(fun,18,200)
Thanks

Antworten (2)

Roger Stafford
Roger Stafford am 21 Aug. 2014

1 Stimme

Your function definition is lacking operators in at least four places, one after "cpn(2)", one after "cpn(4)", and one before each of the two 2's. As such it does not constitute a valid matlab function definition.

4 Kommentare

adfnm
adfnm am 21 Aug. 2014
Okay, so how should it look? I am very sorry for my ignorance, but I have no idea hoe to correct this. Also, the fun(100) and fun(200) do give valid answers when the code runs.
adfnm
adfnm am 21 Aug. 2014
Sorry, I do have those in the code
clc
clear all
close all
cpn=[29105 8614.9 1701.6 103.47 909.79];
fun=@ (ll) cpn(1)+cpn(2)*((cpn(3)./ll)/sinh(cpn(3)./ll))^2+cpn(4)*((cpn(5)./ll)/cosh(cpn(5)./ll))^2
fun(100)
fun(200)
quad(fun,100,2000)
Roger Stafford
Roger Stafford am 21 Aug. 2014
That looks like something that would work properly. Does it work, and is it the function you intended as the integrand?
adfnm
adfnm am 21 Aug. 2014
Yes, that is the function. fun(100) returns a scalar, thats why I am baffled to the cause of this error

Melden Sie sich an, um zu kommentieren.

Iain
Iain am 21 Aug. 2014

0 Stimmen

I guess that you've missed off declaring a few operations as elementwise operations, and it's getting out a square matrix instead of a vector.
Each divide, multiply and power should have a "." just before them.

2 Kommentare

adfnm
adfnm am 21 Aug. 2014
fun(100) returns a scalar, thats why I am baffled to the cause of this error, It is not the function that is the problem!
The documentation of 'quad' at
http://www.mathworks.com/help/matlab/ref/quad.html
gives you the answer: "The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x."
This means that 'quad' doesn't send out just scalar quantities to be evaluated. It asks for entire vectors in x to be evaluated to the corresponding vectors y. That means that your expression which works for scalars but is inappropriate for vectors, will not succeed in 'quad'. You must put in those dots as Iain has recommended if you are to avoid the error messages and obtain a correct result.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 21 Aug. 2014

Kommentiert:

am 21 Aug. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by