"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
Show older comments
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
Dyuman Joshi
on 21 Dec 2023
Running the code above does not produce that (or any) error.
Have you are calling the funciton cfunc?
Andrei-Eduard
on 21 Dec 2023
Edited: Andrei-Eduard
on 21 Dec 2023
Walter Roberson
on 21 Dec 2023
Edited: Walter Roberson
on 21 Dec 2023
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.
Andrei-Eduard
on 21 Dec 2023
Dyuman Joshi
on 21 Dec 2023
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.
Voss
on 21 Dec 2023
@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.
Andrei-Eduard
on 21 Dec 2023
Torsten
on 21 Dec 2023
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:
Dyuman Joshi
on 21 Dec 2023
+1 to Torsten's suggestion of MATLAB Onramp
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);
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
1 Comment
Andrei-Eduard
on 21 Dec 2023
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!