How to write a vector-valued function with vector input from the screen in MATLAB?

4 views (last 30 days)
I am trying to do the following in MATLAB:
1. input *from screen* a small positive integer M (between 1 and 8);
2. input *from screen* M function expressions with variables `t, x1, x2, ... xM`; name for now these M functions f1,f2,...,fM;
3. given a vector `v=[t,x1,x2,...,xM]`, evaluate each of the M functions with the input `v`;
4. the output is a vector `Y=[y1,y2,...,yM]` where `y1=f1(v),y2=f2(v),...`.
I learned from this [old code][1] that I can do (1) and (2) with
TRUE = 1;
FALSE = 0;
OK = FALSE;
while OK == FALSE
fprintf(1,'Input the number of equations\n');
M = input(' ');
if M <= 0 | M > 7
fprintf(1,'Number must be a positive integer < 8\n');
else
OK = TRUE;
end;
end;
ss = cell(M,1);
for I = 1:M
fprintf(1,'Input the function F_(%d) in terms of t and y1 ... y%d\n', I,M);
fprintf(1,'For example: ''y1-t^2+1'' \n');
kk = input(' ');
ss{I} = kk;
end;
How can I do (3) and (4)?
[1]: https://www.mathworks.com/matlabcentral/answers/uploaded_files/97690/ALG057.m

Accepted Answer

jack
jack on 21 Dec 2017
I have just found that (3) and (4) can be done by the following code
TRUE = 1;
FALSE = 0;
OK = FALSE;
while OK == FALSE
fprintf(1,'Input the number of equations\n');
M = input(' ');
if M <= 0 || M > 7
fprintf(1,'Number must be a positive integer < 8\n');
else
OK = TRUE;
end
end
ss = cell(M,1);
for I = 1:M
fprintf(1,'Input the function F_(%d) in terms of t and y1 ... y%d\n',...
I,M);
fprintf(1,'For example: ''y1-t^2+1'' \n');
kk = input(' ');
ss{I} = kk;
end
%(3)
V = 1:M+1; %replacing V with other input vectors here
c = num2cell(V);
Y = zeros(1,M);
if M==1
ysym = sym('y1');
else
ysym = sym('y%d',[1,M]);
end
syms t
for I = 1:M
z = evalin(symengine, ss{I});
f = symfun(z,[t,ysym]);
Y(I)=f(c{:});
end
%(4)
Y

More Answers (0)

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!