Solving an equation with 2 known arrays

7 views (last 30 days)
I'm trying to solve equation with 1 variable (k) but 2 vectors (x and t).
I need to get an array of answers for k, such as (k[i] = eqn(t[i],x[i]).
This is what I wrote:
t = [28.35 53.34 85.39 132.7];
x = linspace(0.01,0.04,4);
k = sym ('k',[1 4]);
eqn = T-Ti==(2*q*sqrt((t.*k)/(c*p*pi))./k)*exp(((-x.^2)*c*p)./(4*k.*t))-((q*x)./k)*erfc(x/(2*sqrt((t.*k)/(c*p))));
solk = solve(eqn,k);
solk.k1
solk.k2
solk.k3
solk.k4
and of course it doesn't work.
How do I fix it?

Answers (1)

Sreeranj Jayadevan
Sreeranj Jayadevan on 30 Dec 2020
Your code is working as expected. I was able to execute the same when a different equation was used. Check if the equation you are using contain dimensional mismatches.
The following code works:
x=sym('x', [1 4]);
y=[1 2 3 4];
z=[2 3 4 5];
sol=solve(x.*y+z+1==0,x); % You can change this equation
sol.x1
sol.x2
sol.x3
sol.x4
% Converting it into an array of type double
temp=struct2cell(sol);
sol_arr=double([temp{:}]);
"sol_arr" returns the array of answers that you require.

Categories

Find more on Mathematics 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!