"Execution of script cfunc as a function is not supported" i don't understand why i get this error, i tried making a function on multiple intervals

L=0;
A1=-0.0036;
A2=-0089;
A3=8.75e-4;
B1=1.8e-5;
B2=2.7188e-5;
B3=-4.5e-6;
L1=0.0035;
L2=0.003;
L3=0.0055;
x=(0:0.0001:L3);
function c = func(x,L1,L2,L3)
c1 = @(x)(A1*x+B1);
c2 = @(x)(E/(2*D)*(x^2)+A2*x+B2);
c3 = @(x)(A3*x+B3);
if (x>=0 && x<L1)
c=c1(x);
end
if(x>=L1 && x<L2+L1)
c=c2(x);
end
if(x>=L1+L2 && x<=L1+L2+L3)
c=c3(x);
end
end

9 Comments

Running the code above does not produce that (or any) error.
Have you are calling the funciton cfunc?
Yes, but i can't plot it or at least don't know exactly how to. I can write "cfunc" and i get no errors, but when i try to do anything with the script it just tells my that it can't be run as a function.
The func you show here would only work for scalar x, not a vector of x, so you would have to loop or arrayfun (or rewrite func) to get it to work.
The func you show here depends upon A1, B1 and so on, but does not pass those variables into the function. In order to get it to work, you would need to pass those values into the function, or else you would need to rewrite this as a nested function with shared variables.
You do not call func anywhere inside your script.
Ok , thank you, i wish i could understand all that, but i am kind of new to matlab and i don't know how i might do that
1 - You can define the constants (A and B terms) inside the function, or provide them as input, as you have done with L1, L2 and L3.
2 - You can either go through the elements of x one by one and check in which range it falls into, and calculate the value accordingly, or you can call the function cfunc via arrayfun or vectorize the code.
@Andrei-Eduard: Here is some background information related to the considerations Walter brought up:
Also, the error you report concerns "cfunc", but how is "cfunc" defined? It does not appear in the code you posted. Is it the name of the m-file containing the code you posted (i.e., "cfunc.m")? Please share how "cfunc" is defined and how you are trying to call it.
Thank you very much for you answers , i managed to do it, i don't know how to describe how but i did it !
If you are new to MATLAB and you want to know what you are doing, invest 2 hours of your time and pass an introductionary course for free:

Sign in to comment.

Answers (1)

You do not define D or E
L=0;
A1=-0.0036;
A2=-0089;
A3=8.75e-4;
B1=1.8e-5;
B2=2.7188e-5;
B3=-4.5e-6;
L1=0.0035;
L2=0.003;
L3=0.0055;
x=(0:0.0001:L3);
y = arrayfun(@(X)func(X,L1,L2,L3,A1,A2,A3,B1,B2,B3), x);
Unrecognized function or variable 'E'.

Error in solution>@(x)(E/(2*D)*(x^2)+A2*x+B2) (line 17)
c2 = @(x)(E/(2*D)*(x^2)+A2*x+B2);

Error in solution>func (line 23)
c=c2(x);

Error in solution>@(X)func(X,L1,L2,L3,A1,A2,A3,B1,B2,B3) (line 12)
y = arrayfun(@(X)func(X,L1,L2,L3,A1,A2,A3,B1,B2,B3), x);
plot(x, y)
function c = func(x,L1,L2,L3,A1,A2,A3,B1,B2,B3)
c1 = @(x)(A1*x+B1);
c2 = @(x)(E/(2*D)*(x^2)+A2*x+B2);
c3 = @(x)(A3*x+B3);
if (x>=0 && x<L1)
c=c1(x);
end
if(x>=L1 && x<L2+L1)
c=c2(x);
end
if(x>=L1+L2 && x<=L1+L2+L3)
c=c3(x);
end
end

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 21 Dec 2023

Commented:

on 21 Dec 2023

Community Treasure Hunt

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

Start Hunting!