Is there a mistake in my code?

3 views (last 30 days)
Hello everyone,
I need to plot a graph and after many trials and errors I came up with this code:
syms A B
D1= 12*A + 19.7891*B == 78.8820;
D2= 19.7891*A + 449*B == 44700;
sol=solve([D1,D2], [A,B]);
A=sol.A % A= -169.9537
B=sol.B % B= 107.0451
a=exp(A)
x=2.5:0.1:10
y=a*(x.^B);
plot(x,y)
However the graph I end up with is nothing like the graph I need. This is the graph I get:
And this is the graph I need:
I double-checked the values of all the variables and they are all correct, so I'm thinking I must have made a mistake with the syntax or the commands themselves, but I can't figure it out. I would appreciate any help you can give me.

Accepted Answer

Star Strider
Star Strider on 9 Jan 2022
I might be better to estimate the parameters directly —
x = [2.5 6 9]; % Excerpt From Plot
y = [1500 600 400]; % Excerpt From Plot
pwrfcn = @(b,x) b(1) .* x.^b(2); % Objective Function
[B,fv] = fminsearch(@(b)norm(y - pwrfcn(b,x)), rand(2,1))
B = 2×1
1.0e+03 * 3.8860 -0.0010
fv = 5.3873
xv = 2.5:0.1:10;
figure
plot(x, y, 'pk')
hold on
plot(xv, pwrfcn(B, xv), '-b')
hold off
grid
.
  6 Comments
Arsel Tanriverdi
Arsel Tanriverdi on 10 Jan 2022
That looks much easier, thank you both for your time and effort! You've been of great help!
Star Strider
Star Strider on 10 Jan 2022
As always, (our) pleasure!
.

Sign in to comment.

More Answers (0)

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!