필터 지우기
필터 지우기

How can I ask Matlab to adjust a parameter in an equation until the answer becomes equal with a predetermined input value?

조회 수: 3 (최근 30일)
I am trying to compare two heights, a trial and calculated. The trial height of the object is an input to the program, while the program calculates the actual height but I want matlab to compare the both heights by adjusting a parameter in the height equation until both heights become equal. Then, matlab prints the final value of the parameter adjusted in the height equation. Please, can anybody help as I am a novice with very weak programming skills? Thank you.
I don't have any working code as I am still learning from this community hub.
Lewis is my name, a student
  댓글 수: 2
HWIK
HWIK 2021년 11월 25일
You might want to try and detail your question a bit further. Like, how does the "program" calculate the actual height?
Lewis Tamuno-Ibuomi
Lewis Tamuno-Ibuomi 2021년 11월 25일
Alright, the height calculation has an equation: h = V/A (volume/area), where V is fixed but A is varied from a range of values. So, an arbitrary input (trial height, ht) is provided for the programe so that matlab could pick the actual value of A from the range of values that will make h = ht. So, the programme is to search out the actual value of A (area) for any ht given that will make h = ht, and print the correct value of A. Thanks Oliver.

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

채택된 답변

Stephen23
Stephen23 2021년 11월 25일
편집: Stephen23 2021년 11월 25일
Define a simple anonymous function with one input which calculates the difference between your two heights, and supply that function as the first input to FZERO. FZERO will adjust that input until the output (i.e. difference) is zero, exactly as you request.
V = 23;
ht = 5;
fh = @(A) ht - V./A;
A = fzero(fh,[0.2,100])
A = 4.6000
V./A
ans = 5
  댓글 수: 4
Stephen23
Stephen23 2021년 11월 25일
편집: Stephen23 2021년 11월 25일
"I have tried using different inputs for ht and observed, the program only works for one particular value of ht."
It works for me. Lets try it:
V = 23;
ht = 6; % different height value!!!
fh = @(A) ht - V./A;
A = fzero(fh,[0.2,100]) % gives a different area!!!
A = 3.8333
V./A % checking: gives the input height.
ans = 6.0000
Everything seems to be working as expected.
As you did not show the code you tried I have no idea what you did wrong.
Lewis Tamuno-Ibuomi
Lewis Tamuno-Ibuomi 2021년 11월 25일
Thank you very much Stephen. I see now!! Really appreciate this quick response. It works very well!

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

추가 답변 (1개)

HWIK
HWIK 2021년 11월 25일
I'm not sure I quite understand the description but here is my go at it:
function Actual_area = call_it_what_you_want(V,ht,Areas)
[~,idx] = min(abs(ht-V./Areas));
Actual_area = Areas(idx);
end
This will take the value of A that gives the closest height to the value of ht
  댓글 수: 1
Stephen23
Stephen23 2021년 11월 25일
Lewis Tamuno-Ibuomi's incorrectly posted "Answer" moved here:
Thank you Oliver. Did you try any demonstration to test the codes? I would not mind a rough demonstration. Thanks.

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

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by