Afficher commentaires plus anciens
Hy, i'm trying to create a function to calculate some polynomials for a given value (taken as keyboard input). I wrote this code:
function[]= tchebychef(n)
n= keyboard;
C(1,:)=[zeros(1, n-1) -1 0];
S(1, :)=[zeros(1, n) 1];
P =[zeros(1, n-2) -1 0 1];
for k=2:n
C(k,:)= conv(C(k-1,:),C(1,:))-conv(S(k-1,:),P);
S(k,:)=-polyder(C(k,:))/k;
end;
but I get this error:
Error :Function definitions are not permitted in this context.
How can I solve this? Do I have to write my function differently? THX
Réponses (3)
Oleg Komarov
le 8 Mai 2012
0 votes
You should use a dedicated .m file for a function, i.e. the first line of a function is always (except comments):
function out = foo(in1,in2m,...) .
You cannot mix a script with a function.
3 commentaires
Alejo Corsani
le 8 Mai 2012
Sargondjani
le 8 Mai 2012
- make an .m file
- with content:
function [output1,output2,...]=my_function_name(input1,input2,....)
%then calculations:
output1=input1*input2...
end
Save this file as my_function_name.m somewhere that matlab calls it. That's it!!
Now you call the function by simply with:
[y1,y2,...]=my_function_name(x1,x2,...);
for some examples, check out the file exchange
Alejo Corsani
le 8 Mai 2012
Alejo Corsani
le 12 Mai 2012
4 commentaires
Walter Roberson
le 12 Mai 2012
You need to show us your error message.
Alejo Corsani
le 14 Mai 2012
Alejo Corsani
le 14 Mai 2012
Walter Roberson
le 14 Mai 2012
Earlier you showed the line the error occurred, but not the error message. Now you show a different line, and that is a line of code that does not appear in the code you have shown us, and is related to a different function name than you used before.
We need the relevant function's code, and the error message.
Alejo Corsani
le 15 Mai 2012
3 commentaires
Walter Roberson
le 15 Mai 2012
Use the 'same' option to conv()
Alejo Corsani
le 15 Mai 2012
Oleg Komarov
le 15 Mai 2012
conv(C(k-1,:),C(1,:),'same')
For future reference, try to keep one post per question, otherwise it becomes increasingly harder (not yet in this specific case) to follow the evolution of the post and keeps other contributors away.
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!