3 vues (au cours des 30 derniers jours)
Tianlan Yang le 19 Oct 2021
Commenté : Tianlan Yang le 19 Oct 2021
I don't know what is wrong with that code. Here is the screenshot of the issue and the code:
function intGL=gausslegendre (a,b,f,M,varargin)
y = [-1/ sqrt (3),1/ sqrt (3)];
H2 = (b-a)/(2*M);
z = [a:2*H2:b];
zM = (z(1:end -1)+z(2:end ))*0.5;
x = [zM+H2*y(1), zM+H2*y(2)];
f = f(x,varargin {:});
intGL = H2*sum(f);
return
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

per isakson le 19 Oct 2021
Modifié(e) : per isakson le 19 Oct 2021
This returns a numerical result, but it's not an integer.
fun = @(x) exp(-x.^2./2);
int = gausslegendre( 0, 2, fun, 1 )
function intGL=gausslegendre (a,b,f,M,varargin)
y = [-1/sqrt(3),1/sqrt(3)];
H2 = (b-a)/(2*M);
z = [a:2*H2:b];
zM = (z(1:end -1)+z(2:end ))*0.5;
x = [zM+H2*y(1), zM+H2*y(2)];
f = f(x,varargin{:});
intGL = H2*sum(f);
end
• sqrt (3) (with a space) works at the command line but not in a function (R2018b)
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Tianlan Yang le 19 Oct 2021
That really helps. Thank you !!

Connectez-vous pour commenter.

### Plus de réponses (1)

Walter Roberson le 19 Oct 2021
inf(3)
ans = 3×3
Inf Inf Inf Inf Inf Inf Inf Inf Inf
[-1./inf (3)]
ans = 1×2
0 3
If inf(3) returns a 3 x 3 array of inf, then why doesn't [-1./inf (3)] take -1 divided by a 3 x 3 array of inf?
The answer is that there is a space between the inf and the (3), and within [], space is the concatenation operator unless there is an operator before the space
[1 2]
ans = 1×2
1 2
[1-2]
ans = -1
[1 -2] %space before operator but no space between operator and number --> unary operator
ans = 1×2
1 -2
[1 - 2] %space before and after operator --> subtraction
ans = -1
[-1/ sqrt (3),1/ sqrt (3)];
is being treated as
[-1/ sqrt() (3),1/ sqrt() (3)];
which in turn is
[(-1/ sqrt()), (3), (1/ sqrt()), (3)];
and that fails because sqrt() cannot be invoked with no parameters. My example with inf did not complain because inf can be invoked with no parameters (and usually is.)
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Calculus dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by