Can't find error in code.
Mostra commenti meno recenti
I've thrown together something from various forums in an attempt to solve a numerical problem. I'm trying to find the values for N and V for a range of n. There's a syntax error on line 3 and a parse error in line 5, possibly due to incorrect parentheses. I've checked the parentheses many times and I believe they're fine, so I'm not sure what the issue is. Here's the code:
syms N V
for n = 1:5
[solN,solV] = solve(N == 51.4 + log(V^(1/4)*(2*n*(2*n-1))^(n/4)/(10^(16)))
+(1/2)*log((2*N+2*n-1)/(2*n-1)),
V == ((5.9*10^(33)*n)/(2*n*(2*N+2*n-1))^((n+1)/2))^2)
disp(n)
end
Any help would be appreciated.
Risposta accettata
Più risposte (2)
Star Strider
il 10 Dic 2016
When in doubt with a long expression, break it up into its component expressions and check each for matching prens.
This works. I will leave it to you to determine if it gives the correct result:
syms N V
for n = 1:5
Term1 = log(V^(1/4)*(2*n*(2*n-1))^(n/4)/(10^16));
Term2 = (1/2)*log((2*N+2*n-1)/(2*n-1));
solN = solve(N == 51.4 + Term1 + Term2, N);
V = ((5.9*10^(33)*n)/(2*n*(2*N+2*n-1))^((n+1)/2))^2;
Vv(n,:) = subs(V, N, solN);
disp(n)
end
Vv = vpa(Vv, 5)
The loop is causing problems with the code as your originally wrote it, so my changes were necessary for it to run in the loop.
6 Commenti
David Anthony
il 10 Dic 2016
Star Strider
il 10 Dic 2016
My pleasure.
When I corrected my code to understand that you wanted to continue the first line, I ran the loop and kept getting:
Warning: Cannot find explicit solution.
> In solve (line 316)
Since this is not uncommon, (and since I don’t know what problem you’re solving, I didn’t want to use 'IgnoreAnalyticConstraints',true). I decided to give it to fsolve instead.
The Code —
VN = @(N,V,n) [51.4 + log(V^(1/4)*(2*n*(2*n-1))^(n/4)/(10^(16))) +(1/2)*log((2*N+2*n-1)/(2*n-1)), ...
((5.9*10^(33)*n)/(2*n*(2*N+2*n-1))^((n+1)/2))^2];
for n = 1:5
VNsol = fsolve(@(b) VN(b(1),b(2),n), rand(2,1));
Nv(n,:) = VNsol(1);
Vv(n,:) = VNsol(2);
disp(n)
end
fprintf(1,'\t\tN\t\tV\n')
fprintf(1,'\t%7.2f\t%7.4f\n', [Nv, Vv]')
N V
154.30 0.6335
67.57 0.5647
35.15 0.1433
28.87 0.8367
26.14 0.3210
This starts with a new, arbitrary initial parameter estimate vector each time, and the results — while approximately the same — never are the same, so you may want to experiment with different initial estimates. All I can verify is that the code works. I don’t know if it produces the results you want. (Since I actually have no idea what problem you are solving, I can’t apply what expertise I may have to it.)
David Anthony
il 10 Dic 2016
Star Strider
il 10 Dic 2016
My pleasure.
I am using R2016b. There could be version differences.
Run my code with the symbolic code deleted or commented-out. The code I posted worked for me (as I documented). I don’t know the problem you’re solving, so I have no suggestions on how best to solve it.
The initial parameter estimates are important. See if:
VNsol = fsolve(@(b) VN(b(1),b(2),n), [50, 1E+60]);
and:
fprintf(1,'\t%7.2f\t%7.4E\n', [Nv, Vv]')
work.
That is the best I can do.
David Anthony
il 11 Dic 2016
Star Strider
il 11 Dic 2016
As always, my pleasure.
David Goodmanson
il 10 Dic 2016
Modificato: David Goodmanson
il 11 Dic 2016
1 voto
Hi David,
Looks like the parentheses for line 3 aren't correct after all. Needs one more parenthesis at the end of the statement. And V has one too many, not needing the parenthesis at the end..
3 Commenti
David Anthony
il 10 Dic 2016
David Goodmanson
il 10 Dic 2016
Modificato: David Goodmanson
il 10 Dic 2016
I didn't have the right idea and I see what you mean. At this point could you stand to use the three-dot continuation ... at the end of line 3?
David Anthony
il 10 Dic 2016
Modificato: David Anthony
il 10 Dic 2016
Categorie
Scopri di più su Code Performance 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!