Substitute variable into equation using MATLAB
Mostra commenti meno recenti
Firstly, I tried fixed value for the variable alpha and implemented it into the equation and it works properly, seconly, I read a variable form csv file column no. 10 which represents
alpha variable, the reading worked properly but substitute that variable into equation "eqn" does not run as I expect
>> trial3
alpha =
6568×1 table
Var10
________
4.64e-11
4.82e-11
5.65e-08
6.01e-08
6.89e-08
:
1.32e-07
1.06e-07
1.03e-07
1.5e-06
1.52e-06
Display all 6568 rows.
c =
alpha^3 - alpha*(alpha - 1)^2 == 0
ans =
0
>>
The codes ran are as in below:
%% Solve The Porosity
clear
T = readtable('myfile_new.csv','NumHeaderLines',1); % skips the first three rows of data
alpha = T(:,10)
syms alpha
syms phi
eqn = [phi^3 - alpha * (1 - phi)^2]==0;
c = subs (eqn,phi,alpha)
double (c)
Risposte (1)
Walter Roberson
il 13 Dic 2022
T = readtable('myfile_new.csv','NumHeaderLines',1); % skips the first three rows of data
You read into table format, getting out a table object.
alpha = T(:,10)
You use () to reference a table object. The resulting alpha is a table object containing exactly one variable. You did not ask for the content of the table column.
syms alpha
Skipping over some details: that line of code is mostly equivalent to
alpha = sym('alpha')
which at the matlab level replaces the existing table object alpha with whatever is returned by sym() . sym() returns an object of class sym that In this case in part contains a coded reference to a variable named alpha that lives in the sybolic engine. The coded reference would look something like _symans_[[32,0,116203]] . The actual symbolic variable does not exist at the MATLAB level: the MATLAB level only knows how the symbolic engine wants to refer to the symbolic expression (and a very small amount of other information.)
After the syms call, the table object alpha is gone from the workspace, having been replaced with the object that refers to the symbolic engine.
What do you need? Well, you need
%% Solve The Porosity
T = readtable('myfile_new.csv','NumHeaderLines',1); % skips the first three rows of data
alpha_num = T{:,10};
syms alpha
syms phi
eqn = [phi^3 - alpha * (1 - phi)^2]==0;
c = subs(eqn, alpha, alpha_num)
Possibly instead you want
c = subs(eqn, {phi, alpha}, {alpha_num, alpha_num})
but the result would have no free variables.
Caution: you appear to be trying to create an array of equations. If you ask solve() to work on an array of equations, you would be asking solve() to find values that solve all of the equations simultaneously . solve() will not solve each equation one by one: solve() is for simultaneous equations. So you probably want
%% Solve The Porosity
T = readtable('myfile_new.csv','NumHeaderLines',1); % skips the first three rows of data
alpha_num = T{:,10};
syms alpha phi
eqn = [phi^3 - alpha * (1 - phi)^2]==0;
sol_phi = solve(eqn, phi);
c = double(subs(sol_phi(:).', alpha, alpha_num));
The result would be a 6568 x 3 numeric array, one row for each value of alpha. There are three columns because the equation is a cubic in phi. There is one real solution for each negative alpha, a triple 0 solution at alpha=0, one real solution for positive alpha up to but excluding 6.75, and three real solutions for alpha = 6.75 and larger.
Categorie
Scopri di più su Calculus in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!