MATLAB Answers

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

4 views (last 30 days)
Joshua Dominguez
Joshua Dominguez on 13 Oct 2021
Commented: Mathieu NOE on 13 Oct 2021
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

Answers (3)

Rik
Rik on 13 Oct 2021
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 Comment
Joshua Dominguez
Joshua Dominguez on 13 Oct 2021
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

Sign in to comment.


Cris LaPierre
Cris LaPierre on 13 Oct 2021
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 on 13 Oct 2021
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);

Community Treasure Hunt

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

Start Hunting!