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

3 Ansichten (letzte 30 Tage)
Joshua Dominguez
Joshua Dominguez am 13 Okt. 2021
Kommentiert: Mathieu NOE am 13 Okt. 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

Antworten (3)

Rik
Rik am 13 Okt. 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 Kommentar
Joshua Dominguez
Joshua Dominguez am 13 Okt. 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

Melden Sie sich an, um zu kommentieren.


Cris LaPierre
Cris LaPierre am 13 Okt. 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 am 13 Okt. 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);

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by