Symbolic function input non-symbolic parameters - matlab

I need to define a function that returns a symbolic matrix (sym). It takes 4 input parameters- 2 symbolic matrices, and 2 integers. How do I do that?
This is what I was trying to do-
%my function
function F = matrix(F, F4, i, j)
...
F=...;
end
%calling it in a different file
syms M1;
M1 = ...;
syms M2;
M2 = ...;
syms M3;
M3 = matrix(M1,M2,1,2);

I have done this easy test;
function L = test(A,B,c,d)
syms tmp1 tmp2
tmp1 = c;
tmp2 = d;
L = tmp1*A + tmp2*B;
end
where A and B are already symbolic matrices.

Related

How to sum functions in MATLAB under a condition

I'm trying to write a function that sums a bunch of functions and I have no idea what I'm doing wrong. My code:
function [f_max,x_max] = ftot(l,a,E,J,F)
% a and F are arrays
f_max = 0;
b = l-a;
n = length(F);
f1 = 0;
syms x
for j=1:n
y1 = piecewise(x<=a(j),1,x>a(j),0);
y2 = piecewise(x<=a(j),0,x>a(j),1);
f = (F(j)*b(j)*x)*y1 + (F(j)*b(j)*x^3)*y2;
f1 = f1 + f;
end
Basically I want to create a function that is the sum of F(j)*b(j)*x for j=1:n when x<=a and F(j)*b(j)*x^3 when x>a. How can I accomplish that?

MatLAB fsolve calculation

have a problem with MatLAB
How can I view the intermediate calculations for a given function? not only for the desired variable, but also for the equations included in the system for fsolve.
fsolve first eq second eq fsolve system
function solve_syst ()
a_init = -2;
c = 6;
d = 8;
for i = 1:7
b = i + 4;
syst_eq = #(a) syst_3 (a, b, c, d);
a_vih = fsolve (syst_eq, a_init);
a(i) = a_vih
end
first eq
function x1 = syst_1 (a, b) %first eq
x1 = a * 6 + 2 * b;
end
second eq
function x2 = syst_2 (c, d)%second eq
x2 = c * 4 - d * 2;
end
system for solve
function [prov, x1, x2] = syst_3 (a, b, c, d) %system for fsolve
x1 = syst_1 (a, b)
x2 = syst_2 (c, d)
prov = x1 - x2;
end
You may step through your program using the Matlab debugger. Just set a breakpoint in your code where you want to see the intermediate results.
You can view the current content of your variables either in the workspace, or by typing their name in the console.
You can also navigate step by step onwards from your breakpoint through your code as explained here.

How to use output variables of a function as input for another function

I would like to use the output of a function as input for a function that builds a polynom:
here is my code:
function c = interpolation(x, y)
n = length(x);
V = ones(n);
for j = 2:n
V(:,j) = x.*V(:,j-1);
end
c = V \ y;
disp(V)
for i = 0:n-1
fprintf('c%d= %.3f\n', i, c(i+1));
end
polynome(c);
function p = polynome(x)
n = length(x);
for l= 0:n-1
polynome = polynome * x^l;
end
end
The first function alone, works. That means my code works if I comment starting from line 13 to end, and I get the c values, whose number depends from the length of the entered x vector in the beginning.
I want to use that c values, to build a polynom of this form: p(x) = c0 + c^1*x1 + c2^x2 + .... + c(n-1)^x(n-1) and plot that polynom, with the points aswell xi,yi given at the beginning through the 2 vectors as input of the function interpolation.
Can someone help me here?
make a separate function polynome e.g.
function y=polynome(x,c)
y=sum(c.*x.^(0:length(c)-1));
or just use
y=sum(c.*x.^(0:length(c)-1));
to compute the polynome for your coefficients c.
If you have multiple values of x, e.g.
x=[0:.1:3]';
y=repmat(x,1,numel(c)).^repmat((0:numel(c)-1),numel(x),1)*c';
should give you the values of the polynome

How to create symbolic matrix dynamically in MATLAB?

I need to create a symbolic matrix in MATLAB. It can be done statically as
syms a11 a12 a21 a22;
A = [a11 a12; a21 a22];
or using compact syntax as
A = sym('A%d', [2 2]);
However i don't see how any of these syntax's should allow dynamic initialization. I would like to initialize each matrix element individually using two loops. One shot at a possible syntax (it's NOT valid MATLAB syntax) could be
syms a x1;
P = sym(zeros(n,n));
for l = 1:n
for m = 1:n
kl = l; km = m;
P(l,m)(x1) = a*sin(kl*x1)*sin(km*x1);
end
end
where I used the (invalid) syntax P(l,m)(x1) to specify that each element in P is a function of x1. Hence P(2) would be an (n,n) matrix with elements P(l,m) = a*sin(kl*2)*sin(km*2). I know it's possible to construct the P by building the matrix string (A=[...]) on run time and evaluating using eval. Something like
syms a x1;
command = [];
for i = 1:n
for j = 1:n
kl = i; km = j;
command = [command ' + a*sin(' num2str(kl) '*x1)*sin(' num2str(km) '*x1)'];
if(j < n) command = [command ',']; end
end
if(i < n) command = [command ';']; end
end
eval(['P(x1) = [' command '];'])
However, using eval is bad practice, so I see this solution only as a last resort. Is there any correct way to achieve my goal?
NB: I have written the elements P(l,m) = a*sin(kl*x1)*sin(km*x1) to simplify the code. The actual expression is P(l,m) = sin(kl*x1)*sin(km*x1)*epsilon + kl*km*cos(kl*x1)*cos(km*x1).*b + km*cos(km*x1)*sin(kl*x1)-kl*cos(kl*x1)*sin(km*x1)/(kl^2-km^2).
If you're just trying to avoid the for loops, you can use meshgrid, which hides them from you:
syms a x1
n = 3;
x = meshgrid(1:n)*x1; % multiplying by the symbolic x1 makes x symbolic
P = a*sin(x).*sin(x.');
which returns
P =
[ a*sin(x1)^2, a*sin(2*x1)*sin(x1), a*sin(3*x1)*sin(x1)]
[ a*sin(2*x1)*sin(x1), a*sin(2*x1)^2, a*sin(2*x1)*sin(3*x1)]
[ a*sin(3*x1)*sin(x1), a*sin(2*x1)*sin(3*x1), a*sin(3*x1)^2]

How to deal with recursive loop in MATLAB?

I am trying to compute x1^i * x2^j * x3^k * ......
This is my code so far:
for l = 1:N
f = 1;
for i = 0:2
for j = 0:2-i
for k = 0:2-j
for m = 0:2-k
g(l,f) = x1(l)^i*x2(l)^j*x3(l)^k*x4(l)^m;
f = f+1;
end
end
end
end
end
How can I do this easier or without a loop?
I do not have MATLAB on hand here, but what I'd do is make a vector X = [x1, x2, ..., xn] of bases and a vector P = [i, j, k, ..., z] of powers, and then compute prod(power(X, P)).
power() does an element-wise power function, and prod takes the product of every element in the vector.