Error function definition not supported in this context create functions in code file

Trying to do : function nrm = fournorm(x) for a question But it comes up with Error: Function definition not supported in this context. Create functions in code file.

Mhahida Baksh

  • Direct link to this question

 ⋮ 

  • Direct link to this question

Trying to do : function nrm = fournorm(x) for a question

But it comes up with Error: Function definition not supported in this context. Create functions in code file.

  5 个评论

Deepak Gupta

此评论的直接链接

Where are you trying to do it?

Mhahida Baksh

此评论的直接链接

Stephen23

此评论的直接链接

«Matlab on command window»

Not possible.

Functions may be either:

  • defined in a file with the function declaration, or
  • defined as an anonymous function.

Mhahida Baksh

此评论的直接链接

Im trying to solve a maths question and this is what it tells me to do as the first step:

function nrm = fournorm(x) % find the four-norm of the vector x

Stephen23

此评论的直接链接

«…this is what it tells me to do as the first step:»

You need to write that code in a file and save the file using the same name as the function, i.e. fournorm.m.

In MATLAB it is not possible to define functions like that in the command window.

请先登录,再进行评论。


回答(2 个)

Srivardhan Gadila

  • 此回答的直接链接

If you have simpler functions like

function y = cubicPoly(x)

y = x.^3 + x.^2 + x + 1;

end

then you can make use of function_handle and define such functions in command window itself or in a script as follows:

f = @(x) x.^3 + x.^2 + x + 1;

  1 个评论

Steven Lord

此评论的直接链接

To be clearer, you cannot define a function simply by copying and pasting the code starting with the function keyword at the prompt in the MATLAB Command Window. The function keyword is only allowed inside a script, function, or class file.

请先登录,再进行评论。


Mubbara Muhammad Saleem Sheikh

  • 此回答的直接链接

编辑:Rik

2021-10-7

function move=tictac(n). %first computer move in tic-tac-toe. %go for the centre if empty. %otherwise choose the upper left corner. if n=5 move=5; else move=1 end

(Can anyone help me with this.can u tell me why matlab software shows Error: Function definition are not

supported in this context. Functions

can only be created as local or nested

functions in code files.

  5 个评论

Willie Hernandez

此评论的直接链接

Did you figure it out? I’m having a similar issue.

Mubbara Muhammad Saleem Sheikh

此评论的直接链接

Yeah i made a little change in the function

Shweta Dubey

此评论的直接链接

what is the change , i got same issue

Mubbara Muhammad Saleem Sheikh

此评论的直接链接

Yeah i write function in file «tictac.mm» like this. This is code file

Mubbara Muhammad Saleem Sheikh

此评论的直接链接

After this , where we apply function write any value of functions u will get value

请先登录,再进行评论。

发生错误

由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。

Translated by Microsoft

MathWorks - Domain Selector

Below is my code to solve the parallel plate capacitor problem.

clear all

x=linspace(1,20,10);

y=linspace(1,20,10);

[X,Y]=meshgrid(x,y);

C=delaunay(X(:),Y(:));

xy=[X(:) Y(:)];

Np=length(xy);

S=MatGlob(xy,C);

F=sparse(Np,1);

V0=1;

G=1e12;

for k=1:Np;

if x(k)==1

S(k,k)=S(k,k)+G;

F(k)=G*(-V0);

if x(k)==20

S(k,k)=S(k,k)+G;

F(k)=G*(+V0);

end

V=SF;

figure

plot(xy,V,'*-')

title('potential(V)')

xlabel('postition(m)')

ylabel('postition(m)')

hold on

function S=MatGlob(xy,C)

Np=length(xy);

S=sparse(Np,Np);

Epsr=3.5;

Ne=size(C,1);

for k=1:Ne

N=C(k,:);

x1=xy(N(1),1);

x2=xy(N(2),1);

x3=xy(N(3),1);

y1=xy(N(1),2);

y2=xy(N(2),2);

y3=xy(N(3),2);

abc1=[1 x1 y1;1 x2 y2;1 x3 y3][1;0;0];

abc2=[1 x1 y1;1 x2 y2;1 x3 y3][0;1;0];

abc3=[1 x1 y1;1 x2 y2;1 x3 y3][0;0;1];

Grad=[abc1(2) abc2(2) abc3(2); abc1(3) abc2(3) abc3(3)];

Se=Epsr*(Grad'*Grad)*abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/2;

S(N,N)=S(N,N)+Se;

end

end

Yet, Matlab annouce the error as follows: «Function definition not supported in this context. Create functions in code file» Would you please help me to fix this error? Thank you in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 clc; clear; close all;% Очистка памяти и экрана, закрытие окон 
 
 global rSphere mSphere g cxSphere cxWing k; 
 global sWing vSphere sSphere mu0; 
 
 %независимые от слоя параметры 
 rSphere = 0.45; %радиус сферы, м 
 mSphere = 230; %масса сферы, кг 
 g = 9.8066; %ускорение свободного падения, м/с^2 
 cxSphere = 0.47; %коэффициент сопротивления сферы (от числа Рей-нольдса), безразмерный 
 cxWing = 0.045; %коэффициент сопротивления крыла (от числа Рей-нольдса), безразмерный 
 lWing = 0.5; %размах крыла, м 
 lambda = 5; %удлинение крыла 
 k = 0.9*pi; %уточняющий коэффициент 
 c0 = lWing/lambda; %средняя хорда = центральная хорда, м 
 thickOfWing = 16; %относительная максимальная толщина, % 
 sWing = lWing*((c0*thickOfWing)/100); %площадь характерного сече-ния крыла, м^2 
 vSphere = (4/3)*pi*(rSphere.^3); %объем сферы, м^3 
 sSphere = pi*(rSphere.^2); %плошадь сферы, м^2 
 mu0 = (k*c0)/(2*lWing); 
 
 %[H rhoWater W alpha] изменение параметров в слоях% 
 waterLayers = [70 1025 -0.1 -0.2 
 130 1050 0.15 0.2]; 
 %где H - толщина слоя, м; 
 %rhoWater - плотность воды в слое, кг/м^3; 
 %W - скорость сдивого течения, м/с; 
 %alpha0 - %угол между хордой крыла и осью y 
 %первая строка в матрице - первый слой от поверхности, 
 %вторая строка в матрице - второй слой от поверхности и тд.
 
 dt = 1; %шаг изменения времени, c 
 
 figure(1); hold on 
 grid on 
 xlabel('x') 
 ylabel('y') 
 
 %рисуем траектории, не предполагая малость угла delta 
 %рисуем траектории, предполагая малость угла delta 
 waterLayers(:,4) = [0; 0]; 
 DrawTrajectoryAF(@RungeKuzn, waterLayers, dt, 'b'); 
 DrawTrajectoryAF(@Runge, waterLayers, dt, 'g'); 
 
 waterLayers(:,4) = [0.2; 0.2]; 
 DrawTrajectoryAF(@Runge, waterLayers, dt, 'r'); 
 DrawTrajectoryAF(@RungeKuzn, waterLayers, dt, 'y'); 
 hold off 
  
 figure(2); hold on 
 grid on 
 xlabel('x') 
 ylabel('y') 
 
 dt = 1; 
 waterLayers = [70 1025 0 -0.2 
 130 1050 0 0.2]; 
 
 DrawTrajectoryAF(@Runge, waterLayers, dt, 'b'); 
 DrawTrajectoryAF(@Runge, waterLayers, dt, 'r', @AlphaFuncTcos); 
 DrawTrajectoryAF(@Runge, waterLayers, dt, 'g', @AlphaFuncTline); 
 hold off 
  
 figure(3); hold on 
 grid on 
 xlabel('x') 
 ylabel('y') 
 
 dt = 1; 
 waterLayers = [70 1025 0 -0.2 
 130 1050 0 0.2]; 
 
 DrawTrajectoryAF(@Runge, waterLayers, dt, 'r', @AlphaFuncYline); 
 DrawTrajectoryAF(@Runge, waterLayers, dt, 'b', @AlphaFuncXline); 
 hold off 
 
figure(4); hold on 
 grid on 
 xlabel('x') 
 ylabel('y') 
 
 dt = 1; 
 
 DrawTrajectoryAT(@Runge, waterLayers, dt, 'b', @AlphaFuncTable); 
 hold off 
 
 function [alpha0] = AlphaFuncTcos(t,x,y) 
 alpha0 = -0.2*cos(t/12); 
 end 
 
 function [alpha0] = AlphaFuncTline(t,x,y) 
 alpha0 = -0.01*t + 0.2; 
 end 
 
 function [alpha0] = AlphaFuncYline(t,x,y) 
 alpha0 = 0.001*y - 0.2; 
 end 
 
 function [alpha0] = AlphaFuncXline(t,x,y) 
 alpha0 = -0.1*x + 0.2; 
 end 
 
 function [alpha0] = AlphaFuncTable(t,x,y, nOfLayer, alpha0) 
 
 global tIndex xPosIndex xNegIndex yIndex 
 
 %[t alpha0] 
 timeCond = [0 0.2; 10 -0.2; 25 0]; 
 %[nOfLayer x alpha0] 
 xPositiveCond = [1 1 0.1; 1 3 0.05; 1 6 -0.2]; 
 %[nOfLayer x alpha0] 
 xNegativeCond = [2 -0.5 0.1; 2 -2 0.05; 2 4 -0.2]; 
 %[y alpha0] 
 yCond = [5 0.2; 100 0.1; 140 -0.2]; 
 
 if tIndex <= size(timeCond,1) && t >= timeCond (tIndex, 1) 
 alpha0 = timeCond(tIndex , 2); 
 tIndex = tIndex + 1; 
 end 
 
 if yIndex <= size(yCond,1) && y >= yCond (yIndex, 1) 
 alpha0 = yCond(yIndex , 2);
 yIndex = yIndex + 1; 
 end 
 
 if xPosIndex <= size(xPositiveCond,1)&& nOfLayer == xPositiveCond(xPosIndex, 1) && x >= xPositiveCond(xPosIndex, 2) 
 alpha0 = xPositiveCond(xPosIndex , 3); 
 xPosIndex = xPosIndex + 1; 
 end 
 
 if xNegIndex <= size(xNegativeCond,1)&& nOfLayer == xNegativeCond(xNegIndex, 1) && x <= xNegativeCond(xNegIndex, 2) 
 alpha0 = xNegativeCond(xNegIndex , 3); 
 xNegIndex = xNegIndex + 1; 
 end 
 
 end

Понравилась статья? Поделить с друзьями:
  • Error func no 8 hifi image
  • Error func no 11 recovery image error no 2 load failed
  • Error full join is only supported with merge joinable or hash joinable join conditions
  • Error ftp login failed
  • Error ftdiun2k ini not found press finish to exit