So I wasn't clear... I thought the question would have been too long. Here goes:
original problem:
ca=3.96;
c0t(1)=2*ca/10;
c1t(1)=ca;
c2t(1)=20/56;
c3t(1)=20/56;
syms c0 c1 c2 c3 c4 c5 c6 c7 c8
eqn1 = c0(1)+c4(1)+c8(1) == c0t(1);
eqn2 = c1(1)+c4(1)+c5(1)+c6(1)+2*c7(1)+2*c8(1) == c1t(1);
eqn3 = c2t(1)==c2(1)+c5(1);
eqn4 = c3t(1)==c3(1)+c6(1)+c7(1)+c8(1);
eqn5 = c4(1)==c1(1)*c0(1)*10^1.98;
eqn6 = c5(1)==c1(1)*c2(1)*10^2.25;
eqn7 = c6(1)==c1(1)*c3(1)*10^4.04;
eqn8 = c7(1)==c1(1)^2*c3(1)*10^5.38;
eqn9 = c8(1)==c1(1)^2*c0(1)*c3(1)*10^8.1;
sol = solve([eqn1, eqn2, eqn3, eqn4, eqn5, eqn6, eqn7, eqn8, eqn9], [c0, c1, c2, c3, c4, c5, c6, c7, c8]);
c0Sol = sol.c0
c1Sol = sol.c1
c2Sol = sol.c2
c3Sol = sol.c3
c4Sol = sol.c4
c5Sol = sol.c5
c6Sol = sol.c6
c7Sol = sol.c7
c8Sol = sol.c8
AND the result is something like this
c0Sol =
(...*RootOf(z^9 - (... + (...*z^7)/... - (...*z^6)/... + (...*z^5)/... - (...*z^4)/... + (...*z^3)/... - (...*z^2)/... + ...................
This has a few pages of digits and operators. On top of this I get 8 more things for this c0Sol and the same for all other cxSol.
What I need is a number like 1.23 * 10 ^ (-14).
Edit1:
Now I am trying to convert it to fsolve:
root9d.m
function F = root9d(c0,c1,c2,c3,c4,c5,c6,c7,c8)
F = [ c0+c4+c8-3.96*2;
c1+c4+c5+c6+2*c7+2*c8-3.96;
c2+c5-20/56;
c3+c6+c7+c8-20/56;
c1*c0*10^(198/100)-c4;
c1*c2*10^(225/100)-c5;
c1*c3*10^(404/100)-c6;
c1^2*c3*10^(538/100)-c7;
c1^2*c0*c3*10^(81/10)-c8;
2*c1(1)+c4(1)+c6(1)-2*c2(1)-3*c3(1)-c6(1)-c0(1)
];
end
and
fun = #root9d;
x0 = [0,0,0,0,0,0,0,0,0];
x = fsolve(fun,x0)
getting
Error using root9d (line 2)
Not enough input arguments.
Error in fsolve (line 219)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Edit2:
am trying vpasolve now
ca=3.96;
c0t(1)=2*ca/10;
c1t(1)=ca;
c2t(1)=20/56;
c3t(1)=20/56;
syms c0 c1 c2 c3 c4 c5 c6 c7 c8
[sol_c0, sol_c1, sol_c2, sol_c3, sol_c4, sol_c5, sol_c6, sol_c7, sol_c8] = vpasolve([c0(1)+c4(1)+c8(1) == c0t(1), c1(1)+c4(1)+c5(1)+c6(1)+2*c7(1)+2*c8(1) == c1t(1), c2t(1)==c2(1)+c5(1), c3t(1)==c3(1)+c6(1)+c7(1)+c8(1), c4(1)==c1(1)*c0(1)*10^1.98, c5(1)==c1(1)*c2(1)*10^2.25, c6(1)==c1(1)*c3(1)*10^4.04, c7(1)==c1(1)^2*c3(1)*10^5.38, c8(1)==c1(1)^2*c0(1)*c3(1)*10^8.1], [c0, c1, c2, c3, c4, c5, c6, c7, c8])
with 9 results like this
sol_c0 =
-2.1773252072885945825011626975607
-0.002452126418901470788857646210148
- 0.18134710731268774000741555758302 - 0.51377486965659220019753505021528i
-1.7768042958209364363265199765376
1.0547920479103151637363730741365
0.0026459489994495584563346427370604
0.0006200391937591632994332743093457
0.020935185135197845770208454108444
- 0.18134710731268774000741555758302 + 0.51377486965659220019753505021528i
What do these sol_cx each with 9 results mean, I need only 9 numbers.
Anyway I'm glad I can still write something in matlab...
Thank you, Dan.
one way to solve my problem is this one, but I hope I will find a shorter one.... also sorry for not saying they were supposed to be positives....
index = find(sol_c0>=0);
sol_c0 = sol_c0(index); sol_c1 = sol_c1(index); sol_c2 = sol_c2(index); sol_c3 = sol_c3(index);sol_c4 = sol_c4(index);sol_c5 = sol_c5(index);sol_c6 = sol_c6(index);sol_c7 = sol_c7(index);sol_c8 = sol_c8(index);
and all this for every sol_
Related
I tried belows on MATLAB.
A = rand(2, 20);
B = rand(20, 1);
D1 = A * B;
D1 = D1(1);
D2 = A(1, :) * B;
D1 == D2
ans =
0
D1 - D2
ans =
-8.8818e-16
I expected that two results are the exactly same.
What differences between both calc ways?..
Thanks,
I have a problem with the equation shown below. I want to enter a vector in t2 and find the roots of the equation from different values in t2.
t2=[10:10:100]
syms x
p = x^3 + 3*x - t2;
R = solve(p,x)
R1 = vpa(R)
Easy! Don't use syms and use the general formula:
t2 = [10:10:100];
%p = x^3 + 3*x - t2;
a = 1;
b = 0;
c = 3;
d = -t2;
D0 = b*b - 3*a*c;
D1 = 2*b^3 - 9*a*b*c + 27*a^2*d;
C = ((D1+sqrt(D1.^2 - 4*D0.^3))/2).^(1/3);
C1 = C*1;
C2 = C*(-1-sqrt(3)*1i)/2;
C3 = C*(-1+sqrt(3)*1i)/2;
f = -1/(3*a);
x1 = f*(b + C1 + D0./C1);
x2 = f*(b + C2 + D0./C2);
x3 = f*(b + C3 + D0./C3);
Since b = 0, you can simplify this a bit:
% ... polynomial is the same
D0 = -3*a*c;
D1 = 27*a^2*d;
% ... the different C's are the same
f = -1/(3*a);
x1 = f*(C1 + D0./C1);
x2 = f*(C2 + D0./C2);
x3 = f*(C3 + D0./C3);
Trial>> syms x p
Trial>> EQUS = p == x^3 + 3*x - t2
It is unknown that you want to solve an equation or a system.
Suppose that you want to solve a system.
Trial>> solx = solve(Eqns,x)
But, I do not think you can find roots.
You can solve one equation.
Trial>> solx = solve(Eqns(3),x)
As far as I know, Maple can do this batter.
In general, loops should be avoided, but this is the only solution that hit my brain right now.
t2=[10:10:100];
pp=repmat([1,0,3,0],[10,1]);
pp(:,4)=-t2;
for i=1:10
R(i,:) =roots(pp(i,:))';
end
I am getting this error in my code:
Error using odearguments (line 91)
#(T,C)(C1.*((V1.P_OPEN)+V2).(CA_ER-C))-((V3.*(C.^2))/(C.^2+(K3^2)))
must return a column vector.
But in the MATLAB documentation there is an example Example 3 where the vectors are given as input but it works just fine. Why is that I get an error in my code?
This is my code:
Ca_ER = 10e-6;
c0 = 2e-6;
c1 = .185;
v1 = 6;
v2 = .11;
v3 = .09e6;
v4 = 1.2;
k3 = .1e-6;
a1 = 400e6;
a2 = 0.2e6;
a3 = 400e6;
a4 = 0.2e6;
a5 = 20e6;
b2 = .21;
d1 = 0.13e-6;
d2 = b2/a2;
d3 = 943.4e-9;
d4 = d1*d2/d3;
d5 = 82.34e-9;
IP= .5e-6;
Ca=.001e-6:.01e-6:1e-6;
num=Ca.*IP.*d2;
deno= (Ca.*IP+ IP*d2+d1*d2+Ca.*d3).*(Ca+d5);
p_open=( num./deno).^3; %this is the vector input
dc=#(t,c) (c1.*((v1.*p_open)+v2).*(Ca_ER-c))-((v3.*(c.^2))/(c.^2+(k3^2)));
[t,c]=ode45(dc,linspace(0, 100, 1000),.19e-6);
plot(t,c);
The error is exactly that. ode45 needs to return a column vector but your variable Ca is in fact a row vector. Therefore, inside the anonymous function, all processing is done by a row vector and what is returned is a row vector and the error is thus produced.
The culprit is this statement:
Ca=.001e-6:.01e-6:1e-6;
The above syntax is declaring a row vector. Therefore, to transform this into a column vector, simply take the transpose:
Ca=.001e-6:.01e-6:1e-6;
Ca = Ca.'; %// Transpose
I'm trying to solve the a very large system of coupled nonlinear equations. Following this thread and the related help by Matalb (first example) I tried to wrote the following code:
%% FSOLVE TEST #2
clc; clear; close all
%%
global a0 a1 a2 a3 a4 h0 TM JA JB
a0 = 2.0377638272727268;
a1 = -7.105521894545453;
a2 = 9.234000147272726;
a3 = -5.302489919999999;
a4 = 1.1362478399999998;
h0 = 45.5;
TM = 0.00592256;
JA = 1.0253896074561006;
JB = 1.3079437258774012;
%%
global N
N = 5;
XA = 0;
XB = 15;
dX = (XB-XA)/(N-1);
XX = XA:dX:XB;
y0 = JA:(JB-JA)/(N-1):JB;
plot(XX,y0,'o')
[x,fval] = fsolve(#nlsys,y0);
where the function nlsys is as follows:
function S = nlsys(x)
global a1 a2 a3 a4 N TM h0 dX JA JB
H = h0^2/12;
e = cell(N,1);
for i = 2:N-1
D1 = (x(i+1) - x(i-1))./2./dX;
D2 = (x(i+1) + x(i-1) - 2.*x(i))./(dX^2);
f = a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3;
g = - H.* (a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3)./(x(i).^5);
b = (H/2) .* (5*a1 + 8*a2.*x(i) + 9*a3.*x(i).^2 + 8*a4.*x(i).^3)./(x(i).^6);
e{i} = #(x) f + b.*(D1.^2) + g.*D2 - TM;
end
e{1} = #(x) x(1) - JA;
e{N} = #(x) x(N) - JB;
S = #(x) cellfun(#(E) E(x), e);
When I run the program, Matlab gives the following errors:
Error using fsolve (line 280)
FSOLVE requires all values returned by user functions to be of data type double.
Error in fsolve_test2 (line 32)
[x,fval] = fsolve(#nlsys,y0);
Where are my mistakes?
Thanks in advance.
Petrus
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have the following code (part of a larger program) in matlab:
while(k<n)
C.(sprintf('Cnew')) = C.(sprintf('C%d',k));
d1 = equalize_dimension(a.F, C.(sprintf('Cnew')));
distance_new = distance(d1.x, d1.y);
k = k + 1;
end
If you want to substitute values since I have included part of the program, this would be as follows:
C.(sprintf('Cnew')):
78
And, for a.F it is as follows:
78 82 84 80
80 84 86 82
82 87 88 85
82 87 89 86
For the equalize_dimension(x,y) function, it is as follows:
function n = equalize_dimension (x,y)
[r1 c1] = size(x);
[r2 c2] = size(y);
if r1<r2
e= r2-r1;
for i=1:e
x(r1+1,1)=0;
r1 = r1 + 1;
end
[r1 c1] = size(x);
n.x =x;
n.y = y;
end
if r1>r2
e = r1-r2;
for i=1:e
y(r2+1,1)=0;
r2 = r2 + 1;
end
[r2 c2] = size(y);
n.x = x;
n.y = y;
end
if c1<c2
e= c2-c1;
for i=1:e
x(1,c1+1)=0;
c1 = c1 + 1;
end
[r1 c1] = size(x);
n.x = x;
n.y = y;
end
if c1>c2
e = c1-c2;
for i=1:e
y(1,c2+1)=0;
c2 = c2 + 1;
end
[r2 c2] = size(y);
n.x = x;
n.y = y;
end
if r1==r2 && c1==c2
n.x = x;
n.y = y;
end
And, for the distance(x,y) function, it is as follows:
function m = distance(x,y)
[r c] = size(x);
for i=1:r
for j=1:c
summation = (sum(sum(pdist2(x,y))));
end
end
m=summation;
end
When I run the program, I get the following error:
??? Index exceeds matrix dimensions.
Error in ==> fs at 36
distance_new = distance(d1.x, d1.y);
Why is that?
Thanks.
First of all, stop in debugger before line distance_new = distance(d1.x, d1.y);
and type
>> which distance
I suspect that you'll get as output
distance is a variable.
Meaning that you override the function distance by using a variable with the same name.
Second, in function distance what is the reason for the nested loop on i and j? You are not using these variables and summation is computed regardless of the nested loops.
please also try not to use i and j as variables.