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

답변 (3개)

Oleg Komarov
Oleg Komarov 2012년 5월 8일

0 개 추천

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.
More details in Scripts and Functions

댓글 수: 3

Alejo Corsani
Alejo Corsani 2012년 5월 8일
THX...this is my first time with a function!!! XD How should I write it? Thank you very much!!!
Sargondjani
Sargondjani 2012년 5월 8일
- 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
Alejo Corsani 2012년 5월 8일
THX!!!

댓글을 달려면 로그인하십시오.

Alejo Corsani
Alejo Corsani 2012년 5월 12일

0 개 추천

Hy, I wrote a function file (tchebyshef.m) containing this:
function [ C, S ] = tchebyshef( n )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% function[n]= tchebychef;
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;
end
But when i write this:
C= zeros(4,5);
S= zeros(4,5);
[C,S]= tchebyshef(4)
I get this error:
Error in tchebyshef (line 9)
C(k,:) = conv(C(k-1,:),C(1,:))-conv(S(k-1,:),P);
What is my error?

댓글 수: 4

Walter Roberson
Walter Roberson 2012년 5월 12일
You need to show us your error message.
Alejo Corsani
Alejo Corsani 2012년 5월 14일
I entered my error message at the end of my answer. However, this is my errror message:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in tcheby (line 7)
C(1)=[zeros(1, n-1), -1, 0];
Alejo Corsani
Alejo Corsani 2012년 5월 14일
Sorry..I wrote the wrong error message...the correct one is this:
Subscripted assignment dimension mismatch.
Error in tchebyshef (line 9)
C(k,:) = conv(C(k-1,:),C(1,:))-conv(S(k-1,:),P);
Walter Roberson
Walter Roberson 2012년 5월 14일
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
Alejo Corsani 2012년 5월 15일

0 개 추천

Hi, as I said, I wrote a function file (tchebybyshef.m) in which I wrote:
function [ C, S ] = tchebyshef( n )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% function[n]= tchebychef;
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;
end
then in my Matlab Command Window I wrote these three lines:
C= zeros(4,5); % Inizialize C as a matrix of all zeros
S= zeros(4,5); %Inizialize S as a matrix of all zeros
[C,S]= tchebyshef(4)
and this is my Matlab response:
Subscripted assignment dimension mismatch.
Error in tchebyshef (line 9)
C(k,:) = conv(C(k-1,:),C(1,:))-conv(S(k-1,:),P);
Thx for your time!!! :D

댓글 수: 3

Walter Roberson
Walter Roberson 2012년 5월 15일
Use the 'same' option to conv()
Alejo Corsani
Alejo Corsani 2012년 5월 15일
Sorry I'm very new to Matlab...how and where should I use the "same" option? Thank you very much!!!
Oleg Komarov
Oleg Komarov 2012년 5월 15일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2012년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by