There is an apparent error in line 4, and this code only ran once

3 次查看(过去 30 天)
Hello everyone, I am currently trying to figure out what is wrong with my code:
t = [36, 65, 100, 160];
c = [0.145, 0.120, 0.100, 0.080];
t2 = (20:10:160);
c2 = b*exp(t2*m);
plot(t, c)
plot(t, c, '*g', t2, c2, '-g');
P = polyfit(t, log(c), 1);
m = P(1);
b = exp(P(2));
TE = sprintf('C = %0.2fe^{%0.3ft}',b, m);
text(43, 0.085, TE);
I wrote 2 codes that are similar to the one above, but it said there is an error in line 4 (which matlab detected the same line for both codes) after I cleared everything in the command window. Is there an error that I am not aware of? Thanks

回答(3 个)

Rik
Rik 2021-10-13
t = [36, 65, 100, 160];
c = [0.145, 0.120, 0.100, 0.080];
t2 = (20:10:160);
c2 = b*exp(t2*m);
Unrecognized function or variable 'b'.
As you can see, you didn't define the variable b.
  1 个评论
Joshua Dominguez
Joshua Dominguez 2021-10-13
I just ran the code again, and I honestly forgot to run the code first before pluging in the codes for t2 and c2. thanks for your help

请先登录,再进行评论。


Cris LaPierre
Cris LaPierre 2021-10-13
The issue I see is that you are using b in line 4 to calculate c2, but do not define b until line 9. Perhaps you just need to reorganize your code?
t = [36, 65, 100, 160];
c = [0.145, 0.120, 0.100, 0.080];
t2 = (20:10:160);
P = polyfit(t, log(c), 1);
m = P(1);
b = exp(P(2));
c2 = b*exp(t2*m);
plot(t, c, '*g', t2, c2, '-g');
TE = sprintf('C = %0.2fe^{%0.3ft}',b, m);
text(43, 0.085, TE);

Mathieu NOE
Mathieu NOE 2021-10-13
hello
simply b and m are not yet defined when you do in line 4 :
c2 = b*exp(t2*m);
the code should be organized like this :
t = [36, 65, 100, 160];
c = [0.145, 0.120, 0.100, 0.080];
P = polyfit(t, log(c), 1);
m = P(1);
b = exp(P(2));
t2 = (20:10:160);
c2 = b*exp(t2*m);
plot(t, c, 'dr', t2, c2, '-b');
TE = sprintf('C = %0.2fe^{%0.3ft}',b, m);
text(t2(4), c2(4) + 0.004, TE);

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by