Function file with more than one out put and how to use that function file for two different output
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a function
and its derivative
Now I want to creat a function file with both the output so that i can creat one new function file with formula
the function file I created is showing the following error
%%%%%%%%%%%%%%%%%%%%%%%%%%function fils are as follows :
%%%%%%%%%%%%%%%%%%%% f.m %%%%%%
function V = f(x)
V = x^2/2;
end
%%%%%%%%%%%%%%%%%%% %%%
function V = f1(x)
V1= x^2/2;
V=diff(V1,x,1)
end
%%%%%%%%%%%%% FluxR.m %%%
function V = FluxR(a,b)
V = 0.5*(f(a)+f(b))-0.5*max(abs(f1(a)),abs(f1(b)))*(b-a)
end
All the files are kept in the same folder
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x0 = -1;
xf = 1;
N = 50;
h = (xf-x0)/N;
delt = 0.5*h;
lambda = delt/h;
t0 = 0;
tf = 1;
x = zeros(N-1,1);
u0 = zeros(N-1,1);
for j=1:N-1
x(j) = x0+j*h;
if(x(j) <0)
u0(j) =1;
end
if(x(j) > 0)
u0(j) = 0;
end
end
M = fix((tf-t0)/delt);
unew = zeros(N-1,1);
t = t0;
for k=1
unew(1) = u0(1);
unew(N-1) = u0(N-1);
for j=2:N-2
m= u0(j)
lambda
FluxR(u0(j),u0(j+1))
FluxR(u0(j-1),u0(j))
%unew(j) = u0(j) - lambda*(FluxR(u0(j),u0(j+1)) - FluxR(u0(j-1),u0(j)))
end
% plot(x,unew)
% u0 = unew;
% t = t+delt;
end
when I call the function file its showing the following error
Error using diff
Difference order N must be a positive integer scalar.
Error in f1 (line 3)
V=diff(V1,x,1)
Error in FluxR (line 2)
V = 0.5*(f(a)+f(b))-0.5*max(abs(f1(a)),abs(f1(b)))*(b-a)
Error in rs (line 32)
FluxR(u0(j),u0(j+1))
0 Kommentare
Antworten (1)
Niranjan Sundararajan
am 12 Jul. 2023
Hey there,
From what I understand, you are having trouble calculating the first order differential of x^2/2. That is why you are getting the error. In order to solve this, you can simply use the following script:
t = 5; %example
[out, diff_out] = f(t)
function [V, V1] = f(t)
syms x;
func = x^2/2;
diff_func = diff(func, x);
V = subs(func, x, t);
V1 = subs(diff_func, x, t);
end
This way, in one function itself, you get both f and the differential of f, for any input value of t.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Platform and License finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!