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.

 채택된 답변

Star Strider
Star Strider 2022년 1월 9일

0 개 추천

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

Arsel Tanriverdi
Arsel Tanriverdi 2022년 1월 10일
편집: Arsel Tanriverdi 2022년 1월 10일
I'm afraid I can't understand some parts of your solution, would you care to explain what this line means?
[B,fv] = fminsearch(@(b)norm(y - pwrfcn(b,x)), rand(2,1)
[B,fv] = fminsearch(@(b)norm(y - pwrfcn(b,x)), rand(2,1))
fminsearch: rand(2,1) corresponds to the initial point.
Is there any reasons for using syms to solve the equation? It seems to be too much for what you are trying to do...
Arsel Tanriverdi
Arsel Tanriverdi 2022년 1월 10일
I think I understand fminsearch now, thank you.
As for why I used syms, it's the only way I know for solving linear equations. I would love to know if there is a better option.
A more appropriate option would be to use mldivide,\ for example —
D1= 12*A + 19.7891*B == 78.8820;
D2= 19.7891*A + 449*B == 44700;
becomes —
AB = [12 19.7891; 19.7891 449] \ [78.8820; 44700]
AB = 2×1
-169.9536 107.0451
fprintf('A = %10.4f\nB = %10.4f', AB)
A = -169.9536 B = 107.0451
@Hiro — Thank you!
.
Arsel Tanriverdi
Arsel Tanriverdi 2022년 1월 10일
That looks much easier, thank you both for your time and effort! You've been of great help!
Star Strider
Star Strider 2022년 1월 10일
As always, (our) pleasure!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2022년 1월 9일

댓글:

2022년 1월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by